lo icon indicating copy to clipboard operation
lo copied to clipboard

Stop the `forEach` method is what?

Open baagod opened this issue 3 years ago • 3 comments

ids := []int{0, 1, 2, 3, 4, 5}
lo.ForEach(ids, func(item int, index int) {
    fmt.Println("item:", item)
    if item == 2 { // How to stop an iteration?
        return
    }
})

out:

item: 0
item: 1
item: 2
item: 3
item: 4
item: 5

baagod avatar Feb 03 '23 04:02 baagod

panic

dolmen avatar Feb 23 '23 15:02 dolmen

Note that the Go team is discussing with the community on having ForEach builtin with range: https://github.com/golang/go/discussions/56413

dolmen avatar Feb 23 '23 15:02 dolmen

You can do it with lo.EveryBy

kcmvp avatar Mar 29 '23 01:03 kcmvp