mirror of
https://github.com/samber/lo.git
synced 2026-04-22 23:47:11 +08:00
853 B
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 |
|
|
|
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")