Back to Lo

Core Findduplicatesbyerr

docs/data/core-findduplicatesbyerr.md

1.53.0573 B
Original Source

Returns a slice with the first occurrence of each duplicated element by key, preserving order. The iteratee can return an error to stop iteration immediately.

go
result, err := lo.FindDuplicatesByErr([]int{3, 4, 5, 6, 7}, func(i int) (int, error) {
    return i % 3, nil
})
// []int{3, 4}, <nil>

Example with error:

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