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.3 KiB
1.3 KiB
name, slug, sourceRef, category, subCategory, playUrl, variantHelpers, similarHelpers, position, signatures
| name | slug | sourceRef | category | subCategory | playUrl | variantHelpers | similarHelpers | position | signatures | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| MaxByErr | maxbyerr | find.go#L528 | core | find | https://go.dev/play/p/s-63-6_9zqM |
|
|
221 |
|
Searches the maximum value of a collection using the given comparison function. Returns zero value and nil error when empty.
If the comparison function returns an error, iteration stops and the error is returned.
type Point struct{ X int }
max, err := lo.MaxByErr([]Point{{1}, {5}, {3}}, func(a, b Point) (bool, error) {
return a.X > b.X, nil
})
// {5}, <nil>
Example with error:
type Point struct{ X int }
max, err := lo.MaxByErr([]Point{{1}, {5}, {3}}, func(a, b Point) (bool, error) {
if a.X == 5 {
return false, fmt.Errorf("cannot compare with 5")
}
return a.X > b.X, nil
})
// {1}, error("cannot compare with 5")
Note: the comparison function is inconsistent with most languages, since we use the opposite of the usual convention.