This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.
Your feedback helps us improve!
Core - Intersection helpersโ
This page lists all intersection helpers, available in the core package of lo.
Containsโ
Returns true if an element is present in a collection.
present := lo.Contains([]int{0, 1, 2, 3, 4, 5}, 5)
// trueSimilar:Prototype:func Contains[T comparable](collection []T, element T) boolContainsByโ
Returns true if the predicate returns true for any element in the collection.
exists := lo.ContainsBy(
[]int{0, 1, 2, 3},
func(x int) bool {
return x == 3
},
)
// truePrototype:func ContainsBy[T any](collection []T, predicate func(item T) bool) boolEveryโ
Returns true if all elements of a subset are contained in a collection, or if the subset is empty.
ok := lo.Every([]int{0, 1, 2, 3, 4, 5}, []int{0, 2})
// truePrototype:func Every[T comparable](collection []T, subset []T) boolEveryByโ
Returns true if the predicate returns true for all elements in the collection, or if the collection is empty.
ok := lo.EveryBy(
[]int{1, 2, 3, 4},
func(x int) bool {
return x < 5
},
)
// truePrototype:func EveryBy[T any](collection []T, predicate func(item T) bool) boolSomeโ
Returns true if at least one element of a subset is contained in a collection. Returns false for an empty subset.
ok := lo.Some([]int{0, 1, 2, 3, 4, 5}, []int{0, 6})
// truePrototype:func Some[T comparable](collection []T, subset []T) boolSomeByโ
Returns true if the predicate returns true for any element in the collection. Returns false for an empty collection.
ok := lo.SomeBy(
[]int{1, 2, 3, 4},
func(x int) bool {
return x < 3
},
)
// truePrototype:func SomeBy[T any](collection []T, predicate func(item T) bool) boolNoneโ
Returns true if no element of a subset is contained in a collection, or if the subset is empty.
ok := lo.None([]int{0, 1, 2, 3, 4, 5}, []int{-1, 6})
// truePrototype:func None[T comparable](collection []T, subset []T) boolNoneByโ
Returns true if the predicate returns true for none of the elements in the collection, or if the collection is empty.
ok := lo.NoneBy(
[]int{1, 2, 3, 4},
func(x int) bool {
return x < 0
},
)
// truePrototype:func NoneBy[T any](collection []T, predicate func(item T) bool) boolIntersectโ
Returns the intersection between collections.
lo.Intersect([]int{0, 3, 5, 7}, []int{3, 5}, []int{0, 1, 2, 0, 3, 0})
// []int{3}Prototype:func Intersect[T comparable, Slice ~[]T](lists ...Slice) SliceIntersectByโ
Returns the intersection between two collections using a custom key selector function.
transform := func(v int) string {
return strconv.Itoa(v)
}
lo.IntersectBy(transform, []int{0, 3, 5, 7}, []int{3, 5}, []int{0, 1, 2, 0, 3, 0})
// []int{3}Prototype:func IntersectBy[T any, K comparable, Slice ~[]T](transform func(T) K, lists ...Slice) SliceDifferenceโ
Returns the difference between two collections. The first slice contains elements absent from list2; the second contains elements absent from list1.
left, right := lo.Difference([]int{0, 1, 2, 3, 4, 5}, []int{0, 2, 6})
// []int{1, 3, 4, 5}, []int{6}Prototype:func Difference[T comparable, Slice ~[]T](list1 Slice, list2 Slice) (Slice, Slice)Unionโ
Returns all distinct elements from given collections while preserving relative order.
lo.Union([]int{0, 1, 2, 3, 4, 5}, []int{0, 2}, []int{0, 10})
// []int{0, 1, 2, 3, 4, 5, 10}Prototype:func Union[T comparable, Slice ~[]T](lists ...Slice) SliceWithoutโ
Returns a slice excluding all given values.
lo.Without([]int{0, 2, 10}, 2)
// []int{0, 10}Prototype:func Without[T comparable, Slice ~[]T](collection Slice, exclude ...T) SliceWithoutByโ
Filters a slice by excluding elements whose extracted keys match any in the exclude list.
type User struct {
ID int
Name string
}
users := []User{
{1, "Alice"},
{2, "Bob"},
{3, "Charlie"},
}
filtered := lo.WithoutBy(users, func(u User) int {
return u.ID
}, 2, 3)
// []User{{1, "Alice"}}Similar:Prototype:func WithoutBy[T any, K comparable, Slice ~[]T](collection Slice, iteratee func(item T) K, exclude ...K) SliceWithoutEmptyโ
Returns a slice excluding zero values. Deprecated: use
Compactinstead.lo.WithoutEmpty([]int{0, 2, 10})
// []int{2, 10}Prototype:func WithoutEmpty[T comparable, Slice ~[]T](collection Slice) SliceWithoutNthโ
Returns a slice excluding the elements at the given indexes.
lo.WithoutNth([]int{-2, -1, 0, 1, 2}, 3)
// []int{-2, -1, 0, 2}Similar:Prototype:func WithoutNth[T comparable, Slice ~[]T](collection Slice, nths ...int) SliceElementsMatchโ
Returns true if lists contain the same set of elements (including empty set). If there are duplicate elements, occurrences must match. Order is not checked.
lo.ElementsMatch([]int{1, 1, 2}, []int{2, 1, 1})
// truePrototype:func ElementsMatch[T comparable, Slice ~[]T](list1 Slice, list2 Slice) boolElementsMatchByโ
Returns true if lists contain the same set of keys computed by the predicate, with matching multiplicities. Order is not checked.
type Item struct{
ID string
}
lo.ElementsMatchBy(
[]Item{{"a"}, {"b"}},
[]Item{{"b"}, {"a"}},
func(i Item) string {
return i.ID
},
)
// truePrototype:func ElementsMatchBy[T any, K comparable](list1 []T, list2 []T, iteratee func(item T) K) bool