Files
lo/docs/data/core-maperr.md
T
Samuel Berthe fcc4f11fb5 💄
2026-03-02 16:06:51 +01:00

1018 B

name, slug, sourceRef, category, subCategory, signatures, variantHelpers, similarHelpers, position
name slug sourceRef category subCategory signatures variantHelpers similarHelpers position
MapErr maperr slice.go#L36 core slice
func MapErr[T any, R any](collection []T, transform func(item T, index int) (R, error)) ([]R, error)
core#slice#maperr
core#slice#map
core#slice#filtermap
core#slice#flatmap
core#slice#rejectmap
parallel#slice#map
11

Transforms each element in a slice to a new type using a function that can return an error. Stops iteration immediately when an error is encountered.

// Error case - stops on first error
result, err := lo.MapErr([]int{1, 2, 3, 4}, func(x int, _ int) (string, error) {
    if x == 3 {
        return "", fmt.Errorf("number 3 is not allowed")
    }
    return strconv.Itoa(x), nil
})
// []string(nil), error("number 3 is not allowed")
// Success case
result, err := lo.MapErr([]int{1, 2, 3, 4}, func(x int, _ int) (string, error) {
    return strconv.Itoa(x), nil
})
// []string{"1", "2", "3", "4"}, nil