Back to Lo

Core Productbyerr

docs/data/core-productbyerr.md

1.53.0623 B
Original Source

Calculates the product of values computed by a predicate. Returns 1 for nil or empty collections.

If the iteratee returns an error, iteration stops and the error is returned.

go
strings := []string{"foo", "bar"}
result, err := lo.ProductByErr(strings, func(item string) (int, error) {
    return len(item), nil
})
// 9, <nil>

Example with error:

go
strings := []string{"foo", "bar", "baz"}
result, err := lo.ProductByErr(strings, func(item string) (int, error) {
    if item == "bar" {
        return 0, fmt.Errorf("bar is not allowed")
    }
    return len(item), nil
})
// 3, error("bar is not allowed")