Back to Lo

Core Repeatbyerr

docs/data/core-repeatbyerr.md

1.53.0505 B
Original Source

Builds a slice by calling the callback N times with the current index. The callback can return an error to stop iteration immediately.

go
result, err := lo.RepeatByErr(5, func(i int) (int, error) {
    return i * i, nil
})
// []int{0, 1, 4, 9, 16}, <nil>

Example with error:

go
result, err := lo.RepeatByErr(5, func(i int) (int, error) {
    if i == 3 {
        return 0, fmt.Errorf("number 3 is not allowed")
    }
    return i * i, nil
})
// []int(nil), error("number 3 is not allowed")