Back to Lo

Core Maperr

docs/data/core-maperr.md

1.53.0623 B
Original Source

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.

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