Files
lo/docs/data/core-finderr.md
T
Samuel Berthe fa095e4b4f fix(doc): fix go playground demo URL (#832)
* fix(doc): fix go playground demo URL

* fix(doc): add more go playground demo URL
2026-03-06 00:09:59 +01:00

1.1 KiB

name, slug, sourceRef, category, subCategory, signatures, playUrl, variantHelpers, similarHelpers, position
name slug sourceRef category subCategory signatures playUrl variantHelpers similarHelpers position
FindErr finderr find.go#L83 core find
func FindErr[T any](collection []T, predicate func(item T) (bool, error)) (T, error)
https://go.dev/play/p/XK-qtpQWXJ9
core#find#find
core#find#findorelse
core#find#findkey
core#find#findindexof
core#slice#filter
41

Searches for an element in a slice based on a predicate that can return an error. Returns the element and nil error if found. Returns zero value and nil error if not found. If the predicate returns an error, iteration stops immediately and returns zero value and the error.

result, err := lo.FindErr([]string{"a", "b", "c", "d"}, func(i string) (bool, error) {
    return i == "b", nil
})
// "b", nil

result, err = lo.FindErr([]string{"foobar"}, func(i string) (bool, error) {
    return i == "b", nil
})
// "", nil

result, err = lo.FindErr([]string{"a", "b", "c"}, func(i string) (bool, error) {
    if i == "b" {
        return false, fmt.Errorf("b is not allowed")
    }
    return i == "b", nil
})
// "", error("b is not allowed")