Back to Lo

Core Omitbyerr

docs/data/core-omitbyerr.md

1.53.0680 B
Original Source

Returns a map of the same type excluding entries that match the predicate. Returns an error if the predicate function fails, stopping iteration immediately.

go
m, err := lo.OmitByErr(
    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")
go
m, err := lo.OmitByErr(
    map[string]int{"foo": 1, "bar": 2, "baz": 3},
    func(key string, value int) (bool, error) {
        return value%2 == 1, nil
    },
)
// map[string]int{"bar": 2}, nil