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 - Find helpersโ
This page lists all search helpers, available in the core package of lo.
IndexOfโ
Returns the index of the first occurrence of a value in a slice, or -1 if not found.
idx := lo.IndexOf([]int{0, 1, 2, 1, 2, 3}, 2)
// 2
idx = lo.IndexOf([]int{0, 1, 2, 1, 2, 3}, 6)
// -1Prototype:func IndexOf[T comparable](collection []T, element T) int
LastIndexOfโ
Returns the index of the last occurrence of a value in a slice, or -1 if not found.
idx := lo.LastIndexOf([]int{0, 1, 2, 1, 2, 3}, 2)
// 4
idx = lo.LastIndexOf([]int{0, 1, 2, 1, 2, 3}, 6)
// -1Similar:Prototype:func LastIndexOf[T comparable](collection []T, element T) int
HasPrefixโ
Returns true if a collection starts with the given prefix slice.
lo.HasPrefix([]int{1, 2, 3, 4}, []int{1, 2})
// truePrototype:func HasPrefix[T comparable](collection []T, prefix []T) bool
HasSuffixโ
Returns true if a collection ends with the given suffix slice.
lo.HasSuffix([]int{1, 2, 3, 4}, []int{3, 4})
// truePrototype:func HasSuffix[T comparable](collection []T, suffix []T) bool
Searches for an element in a slice based on a predicate and returns the element with a boolean indicating success.
value, ok := lo.Find([]string{"a", "b", "c", "d"}, func(i string) bool {
return i == "b"
})
// "b", true
value, ok = lo.Find([]string{"foobar"}, func(i string) bool {
return i == "b"
})
// "", falsePrototype:func Find[T any](collection []T, predicate func(item T) bool) (T, bool)
FindIndexOfโ
Searches for an element based on a predicate and returns the element, its index, and a boolean indicating success.
val, idx, ok := lo.FindIndexOf([]string{"a", "b", "a", "b"}, func(i string) bool {
return i == "b"
})
// "b", 1, trueSimilar:Prototype:func FindIndexOf[T any](collection []T, predicate func(item T) bool) (T, int, bool)
FindLastIndexOfโ
Searches for the last element matching the predicate and returns the element, its index, and a boolean indicating success.
val, idx, ok := lo.FindLastIndexOf([]string{"a", "b", "a", "b"}, func(i string) bool {
return i == "b"
})
// "b", 3, trueSimilar:Prototype:func FindLastIndexOf[T any](collection []T, predicate func(item T) bool) (T, int, bool)
FindOrElseโ
Searches for an element based on a predicate and returns it if found, otherwise returns the fallback.
value := lo.FindOrElse([]string{"a", "b", "c", "d"}, "x", func(i string) bool {
return i == "b"
})
// "b"
value = lo.FindOrElse([]string{"foobar"}, "x", func(i string) bool {
return i == "b"
})
// "x"Prototype:func FindOrElse[T any](collection []T, fallback T, predicate func(item T) bool) T
FindKeyโ
Returns the first key whose value equals the provided value.
k, ok := lo.FindKey(map[string]int{"foo":1, "bar":2, "baz":3}, 2)
// "bar", truePrototype:func FindKey[K comparable, V comparable](object map[K]V, value V) (K, bool)
FindKeyByโ
Returns the first key in the map for which the predicate returns true.
k, ok := lo.FindKeyBy(map[string]int{"foo":1, "bar":2, "baz":3}, func(k string, v int) bool {
return k == "foo"
})
// "foo", trueSimilar:Prototype:func FindKeyBy[K comparable, V any](object map[K]V, predicate func(key K, value V) bool) (K, bool)
FindUniquesโ
Returns a slice with elements that appear only once in the collection, preserving original order.
lo.FindUniques([]int{1, 2, 2, 1, 2, 3})
// []int{3}Prototype:func FindUniques[T comparable, Slice ~[]T](collection Slice) Slice
FindUniquesByโ
Returns a slice of elements that are unique by a computed key, preserving original order.
lo.FindUniquesBy([]int{3, 4, 5, 6, 7}, func(i int) int {
return i % 3
})
// []int{5}Prototype:func FindUniquesBy[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) U) Slice
FindDuplicatesโ
Returns a slice with the first occurrence of each duplicated element in the collection, preserving order.
lo.FindDuplicates([]int{1, 2, 2, 1, 2, 3})
// []int{1, 2}Similar:Prototype:func FindDuplicates[T comparable, Slice ~[]T](collection Slice) Slice
FindDuplicatesByโ
Returns a slice with the first occurrence of each duplicated element by key, preserving order.
lo.FindDuplicatesBy([]int{3, 4, 5, 6, 7}, func(i int) int {
return i % 3
})
// []int{3, 4}Prototype:func FindDuplicatesBy[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) U) Slice
Returns the minimum value of a collection. Returns the zero value when the collection is empty.
lo.Min([]int{1, 2, 3})
// 1Prototype:func Min[T constraints.Ordered](collection []T) T
MinIndexโ
Returns the minimum value and its index. Returns (zero value, -1) when the collection is empty.
value, idx := lo.MinIndex([]int{2, 5, 3})
// value == 2, idx == 0Prototype:func MinIndex[T constraints.Ordered](collection []T) (T, int)
MinByโ
Searches the minimum value of a collection using the given comparison function. Returns the first minimal value; zero value when empty.
type Point struct{ X int }
min := lo.MinBy([]Point{{1}, {5}, {3}}, func(a, b Point) bool {
return a.X < b.X
})
// {1}Prototype:func MinBy[T any](collection []T, comparison func(a T, b T) bool) T
MinIndexByโ
Searches the minimum value using a comparison function and returns the value and its index. Returns (zero value, -1) when empty.
type Point struct{ X int }
value, idx := lo.MinIndexBy([]Point{{1}, {5}, {3}}, func(a, b Point) bool {
return a.X < b.X
})
// value == {1}, idx == 0Prototype:func MinIndexBy[T any](collection []T, comparison func(a T, b T) bool) (T, int)
Earliestโ
Searches the minimum time.Time in the provided arguments. Returns zero value when the input is empty.
t1 := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
t2 := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
min := lo.Earliest(t2, t1)
// 2023-01-01 00:00:00 +0000 UTCPrototype:func Earliest(times ...time.Time) time.Time
EarliestByโ
Searches a collection for the element with the minimum time extracted by the predicate. Returns zero value when the collection is empty.
type Event struct{ At time.Time }
events := []Event{{At: time.Now().Add(2 * time.Hour)}, {At: time.Now()}}
first := lo.EarliestBy(events, func(e Event) time.Time {
return e.At
})Prototype:func EarliestBy[T any](collection []T, iteratee func(item T) time.Time) T
Searches the maximum value of a collection. Returns zero value when the collection is empty.
max := lo.Max([]int{2, 5, 3})
// 5Prototype:func Max[T constraints.Ordered](collection []T) T
MaxIndexโ
Returns the maximum value and its index. Returns (zero value, -1) when the collection is empty.
value, idx := lo.MaxIndex([]int{2, 5, 3})
// value == 5, idx == 1Prototype:func MaxIndex[T constraints.Ordered](collection []T) (T, int)
MaxByโ
Searches the maximum value of a collection using the given comparison function. Returns zero value when the collection is empty.
type Point struct{ X int }
max := lo.MaxBy([]Point{{1}, {5}, {3}}, func(a, b Point) bool {
return a.X > b.X
})
// {5}Prototype:func MaxBy[T any](collection []T, comparison func(a T, b T) bool) T
MaxIndexByโ
Returns the maximum value and its index using the given comparison function. Returns (zero value, -1) when the collection is empty.
type Point struct{ X int }
value, idx := lo.MaxIndexBy([]Point{{1}, {5}, {3}}, func(a, b Point) bool {
return a.X > b.X
})
// value == {5}, idx == 1Prototype:func MaxIndexBy[T any](collection []T, comparison func(a T, b T) bool) (T, int)
Latestโ
Searches the maximum time.Time in the provided arguments. Returns zero value when the input is empty.
t1 := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
t2 := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
max := lo.Latest(t1, t2)
// 2024-01-01 00:00:00 +0000 UTCPrototype:func Latest(times ...time.Time) time.Time
LatestByโ
Searches a collection for the element with the maximum time extracted by the predicate. Returns zero value when the collection is empty.
type Event struct{ At time.Time }
events := []Event{{At: time.Now()}, {At: time.Now().Add(2 * time.Hour)}}
last := lo.LatestBy(events, func(e Event) time.Time {
return e.At
})Prototype:func LatestBy[T any](collection []T, iteratee func(item T) time.Time) T
Firstโ
Returns the first element of a collection and whether it exists.
v, ok := lo.First([]int{1, 2, 3})
// v == 1, ok == truePrototype:func First[T any](collection []T) (T, bool)
FirstOrEmptyโ
Returns the first element of a collection or the zero value if empty.
v := lo.FirstOrEmpty([]int{})
// v == 0Prototype:func FirstOrEmpty[T any](collection []T) T
FirstOrโ
Returns the first element of a collection or the fallback value if empty.
v := lo.FirstOr([]int{}, -1)
// v == -1Similar:Prototype:func FirstOr[T any](collection []T, fallback T) T
Returns the last element of a collection and whether it exists.
v, ok := lo.Last([]int{1, 2, 3})
// v == 3, ok == trueSimilar:Prototype:func Last[T any](collection []T) (T, bool)
LastOrEmptyโ
Returns the last element of a collection or the zero value if empty.
v := lo.LastOrEmpty([]int{})
// v == 0Similar:Prototype:func LastOrEmpty[T any](collection []T) T
LastOrโ
Returns the last element of a collection or the fallback value if empty.
v := lo.LastOr([]int{}, -1)
// v == -1Similar:Prototype:func LastOr[T any](collection []T, fallback T) T
Returns the element at index nth of collection. If nth is negative, returns the nth element from the end. Returns an error when nth is out of slice bounds.
v, _ := lo.Nth([]int{10, 20, 30}, 1)
// v == 20Prototype:func Nth[T any, N constraints.Integer](collection []T, nth N) (T, error)
NthOrโ
Returns the element at index nth of collection, or the fallback if out of bounds. If nth is negative, returns the nth element from the end.
v := lo.NthOr([]int{10, 20, 30}, 10, -1)
// v == -1Similar:Prototype:func NthOr[T any, N constraints.Integer](collection []T, nth N, fallback T) T
NthOrEmptyโ
Returns the element at index nth of collection, or the zero value if out of bounds. If nth is negative, returns the nth element from the end.
v := lo.NthOrEmpty([]int{10, 20, 30}, 10)
// v == 0Similar:Prototype:func NthOrEmpty[T any, N constraints.Integer](collection []T, nth N) T
Sampleโ
Returns a random item from a collection.
v := lo.Sample(
[]int{10, 20, 30},
)Prototype:func Sample[T any](collection []T) T
SampleByโ
Returns a random item from a collection, using the provided random index generator.
v := lo.SampleBy([]int{10, 20, 30}, func(n int) int {
return 0
})
// v == 10Prototype:func SampleBy[T any](collection []T, randomIntGenerator randomIntGenerator) T
Samplesโ
Returns N random unique items from a collection.
v := lo.Samples(
[]int{10, 20, 30},
2,
)Prototype:func Samples[T any, Slice ~[]T](collection Slice, count int) Slice
SamplesByโ
Returns N random unique items from a collection, using the provided random index generator.
v := lo.SamplesBy(
[]int{10, 20, 30},
2,
func(n int) int {
return 0
},
)Prototype:func SamplesBy[T any, Slice ~[]T](collection Slice, count int, randomIntGenerator randomIntGenerator) Slice