Files
lo/docs/data/core-countbyerr.md
T
Samuel Berthe fa095e4b4f fix(doc): fix go playground demo URL (#832)
* fix(doc): fix go playground demo URL

* fix(doc): add more go playground demo URL
2026-03-06 00:09:59 +01:00

890 B

name, slug, sourceRef, category, subCategory, signatures, variantHelpers, playUrl, similarHelpers, position
name slug sourceRef category subCategory signatures variantHelpers playUrl similarHelpers position
CountByErr countbyerr slice.go#L863 core slice
func CountByErr[T any](collection []T, predicate func(item T) (bool, error)) (int, error)
core#slice#countbyerr
https://go.dev/play/p/7BnyPhpG6lW
core#slice#countby
core#slice#count
core#slice#everybyerr
core#slice#somebyerr
5

Counts the number of elements for which the predicate is true. Returns an error if the predicate function fails, stopping iteration immediately.

count, err := lo.CountByErr([]int{1, 5, 1}, func(i int) (bool, error) {
    if i == 5 {
        return false, fmt.Errorf("5 not allowed")
    }
    return i < 4, nil
})
// 0, error("5 not allowed")
count, err := lo.CountByErr([]int{1, 5, 1}, func(i int) (bool, error) {
    return i < 4, nil
})
// 2, nil