mirror of
https://github.com/samber/lo.git
synced 2026-04-22 15:37:14 +08:00
fa095e4b4f
* fix(doc): fix go playground demo URL * fix(doc): add more go playground demo URL
1.1 KiB
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 |
|
https://go.dev/play/p/XK-qtpQWXJ9 |
|
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")