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

853 B

name, slug, sourceRef, category, subCategory, signatures, variantHelpers, similarHelpers, position
name slug sourceRef category subCategory signatures variantHelpers similarHelpers position
RepeatByErr repeatbyerr slice.go#L564 core slice
func RepeatByErr[T any](count int, callback func(index int) (T, error)) ([]T, error)
core#slice#repeatbyerr
core#slice#repeatby
core#slice#times
core#slice#repeat
225

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

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

Example with error:

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")