Back to Lo

Core Finderr

docs/data/core-finderr.md

1.53.0747 B
Original Source

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.

go
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")