lo
lo copied to clipboard
Stop the `forEach` method is what?
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
panic
Note that the Go team is discussing with the community on having ForEach builtin with range: https://github.com/golang/go/discussions/56413
You can do it with lo.EveryBy