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.2 KiB
1.2 KiB
name, slug, sourceRef, category, subCategory, playUrl, signatures, variantHelpers, similarHelpers, position
| name | slug | sourceRef | category | subCategory | playUrl | signatures | variantHelpers | similarHelpers | position | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| GroupByErr | groupbyerr | slice.go#L279 | core | slice | https://go.dev/play/p/BzKPcY3AdX2 |
|
|
|
121 |
Groups elements by a key computed from each element using an iteratee that can return an error. Stops iteration immediately when an error is encountered. The result is a map keyed by the group key with slices of original elements.
// Error case - stops on first error
result, err := lo.GroupByErr([]int{0, 1, 2, 3, 4, 5}, func(i int) (int, error) {
if i == 3 {
return 0, fmt.Errorf("number 3 is not allowed")
}
return i % 3, nil
})
// map[int][]int(nil), error("number 3 is not allowed")
// Success case
result, err := lo.GroupByErr([]int{0, 1, 2, 3, 4, 5}, func(i int) (int, error) {
return i % 3, nil
})
// map[int][]int{0: {0, 3}, 1: {1, 4}, 2: {2, 5}}, nil