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.6 KiB
1.6 KiB
name, slug, sourceRef, category, subCategory, playUrl, signatures, variantHelpers, similarHelpers, position
| name | slug | sourceRef | category | subCategory | playUrl | signatures | variantHelpers | similarHelpers | position | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| MinIndexByErr | minindexbyerr | find.go#L404 | core | find | https://go.dev/play/p/MUqi_NvTKM1 |
|
|
|
171 |
Searches the minimum value using a comparison function and returns the value and its index. Returns (zero value, -1, nil) when empty. Stops iteration immediately when an error is encountered.
type Point struct{ X int }
value, idx, err := lo.MinIndexByErr([]Point{{1}, {5}, {3}}, func(a, b Point) (bool, error) {
return a.X < b.X, nil
})
// value == {1}, idx == 0, err == nil
// Error case - stops on first error
_, _, err := lo.MinIndexByErr([]Point{{1}, {5}, {0}}, func(a, b Point) (bool, error) {
if a.X == 0 || b.X == 0 {
return false, fmt.Errorf("zero value not allowed")
}
return a.X < b.X, nil
})
// error("zero value not allowed")
// Error case on first comparison
_, _, err := lo.MinIndexByErr([]Point{{1}, {5}}, func(a, b Point) (bool, error) {
return false, fmt.Errorf("comparison error")
})
// error("comparison error")