Files
lo/docs/data/core-groupbyerr.md
T
Samuel Berthe fa095e4b4f fix(doc): fix go playground demo URL (#832)
* fix(doc): fix go playground demo URL

* fix(doc): add more go playground demo URL
2026-03-06 00:09:59 +01:00

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
func GroupByErr[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) (U, error)) (map[U]Slice, error)
core#slice#groupbyerr
core#slice#groupby
core#slice#groupbymap
core#slice#partitionby
core#slice#keyby
parallel#slice#groupby
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