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

1.0 KiB

name, slug, sourceRef, category, subCategory, signatures, variantHelpers, similarHelpers, position
name slug sourceRef category subCategory signatures variantHelpers similarHelpers position
PickByErr pickbyerr map.go#L117 core map
func PickByErr[K comparable, V any, Map ~map[K]V](in Map, predicate func(key K, value V) (bool, error)) (Map, error)
core#map#pickbyerr
core#map#pickby
core#map#omitby
core#map#omitbyerr
65

Returns a map of the same type filtered by a key/value predicate. Returns an error if the predicate function fails, stopping iteration immediately.

m, err := lo.PickByErr(
    map[string]int{"foo": 1, "bar": 2, "baz": 3},
    func(key string, value int) (bool, error) {
        if key == "bar" {
            return false, fmt.Errorf("bar not allowed")
        }
        return value%2 == 1, nil
    },
)
// map[string]int(nil), error("bar not allowed")
m, err := lo.PickByErr(
    map[string]int{"foo": 1, "bar": 2, "baz": 3},
    func(key string, value int) (bool, error) {
        return value%2 == 1, nil
    },
)
// map[string]int{"foo": 1, "baz": 3}, nil