Back to Lo

Core Zipbyerrx

docs/data/core-zipbyerrx.md

1.53.0826 B
Original Source

Zips multiple slices and projects each grouped set through a function that can return an error. Stops iteration immediately when an error is encountered and returns the zero value (nil for slices).

Variants: ZipByErr2..ZipByErr9

go
result, err := lo.ZipByErr2([]string{"a", "b"}, []int{1, 2}, func(a string, b int) (string, error) {
    if b == 2 {
        return "", fmt.Errorf("number 2 is not allowed")
    }
    return fmt.Sprintf("%s-%d", a, b), nil
})
// []string(nil), error("number 2 is not allowed")
go
result, err := lo.ZipByErr2([]string{"a", "b"}, []int{1, 2}, func(a string, b int) (string, error) {
    return fmt.Sprintf("%s-%d", a, b), nil
})
// []string{"a-1", "b-2"}, nil

When collections are different sizes, the missing attributes are filled with zero value before calling the iteratee.