Files
lo/docs/data/core-keybyerr.md
T
2026-03-02 16:06:51 +01:00

1.1 KiB

name, slug, sourceRef, category, subCategory, signatures, variantHelpers, similarHelpers, position
name slug sourceRef category subCategory signatures variantHelpers similarHelpers position
KeyByErr keybyerr slice.go#L576 core slice
func KeyByErr[K comparable, V any](collection []V, iteratee func(item V) (K, error)) (map[K]V, error)
core#slice#keybyerr
core#slice#keyby
core#slice#groupby
core#slice#groupbyerr
core#slice#partitionby
core#map#associate
core#slice#keyify
231

Transforms a slice to a map using a pivot callback to compute keys. Stops iteration immediately when an error is encountered.

// Error case - stops on first error
result, err := lo.KeyByErr([]string{"a", "aa", "aaa", ""}, func(str string) (int, error) {
    if str == "" {
        return 0, fmt.Errorf("empty string not allowed")
    }
    return len(str), nil
})
// map[int]string(nil), error("empty string not allowed")
// Success case
result, err := lo.KeyByErr([]string{"a", "aa", "aaa"}, func(str string) (int, error) {
    if str == "" {
        return 0, fmt.Errorf("empty string not allowed")
    }
    return len(str), nil
})
// map[int]string{1: "a", 2: "aa", 3: "aaa"}, nil