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

974 B

name, slug, sourceRef, category, subCategory, signatures, variantHelpers, similarHelpers, position
name slug sourceRef category subCategory signatures variantHelpers similarHelpers position
MapKeysErr mapkeyserr map.go#L293 core map
func MapKeysErr[K comparable, V any, R comparable](in map[K]V, iteratee func(value V, key K) (R, error)) (map[R]V, error)
core#map#mapkeyserr
core#map#mapkeys
core#map#mapvalueserr
185

Transforms map keys using a predicate while keeping values. Returns an error if the iteratee function fails, stopping iteration immediately.

in := map[int]int{1: 1, 2: 2, 3: 3}
out, err := lo.MapKeysErr(in, func(v int, _ int) (string, error) {
    if v == 2 {
        return "", fmt.Errorf("even number not allowed")
    }
    return strconv.Itoa(v), nil
})
// map[string]int(nil), error("even number not allowed")
in := map[int]int{1: 1, 2: 2, 3: 3}
out, err := lo.MapKeysErr(in, func(v int, _ int) (string, error) {
    return strconv.Itoa(v), nil
})
// map[string]int{"1":1, "2":2, "3":3}, nil