Files
lo/docs/data/parallel-foreach.md
T
Nathan Baulch e7386d9246 lint: fix inconsistent callback function parameter names (#730)
* Fix linting

* Use is.ElementsMatch

This will ignore the ordering of the final intersection. Especially
important when checking old versions of go that do not guarantee an order
when iterating through maps.

* lint: fix inconsistent callback function parameter names

* lint: rename "iteratee" to "transform" for *Map helpers

* lint: rename "project" parameters to "transform"

* lint: rename "cb" parameters to "callback"

* lint: rename "iteratee" to "callback" for ForEach helpers

---------

Co-authored-by: Franky W. <frankywahl@users.noreply.github.com>
Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
2025-11-06 18:05:11 +01:00

45 lines
911 B
Markdown

---
name: ForEach
slug: foreach
sourceRef: parallel/slice.go#L32
category: parallel
subCategory: slice
playUrl: https://go.dev/play/p/sCJaB3quRMC
similarHelpers:
- core#slice#foreach
- parallel#slice#map
position: 10
signatures:
- "func ForEach[T any](collection []T, callback func(item T, index int))"
variantHelpers:
- parallel#slice#foreach
---
Iterates over elements of a collection and invokes the callback for each element in parallel.
```go
import (
"fmt"
lop "github.com/samber/lo/parallel"
)
lop.ForEach([]string{"hello", "world"}, func(x string, _ int) {
fmt.Println(x)
})
// prints lines in any order depending on scheduling
```
Useful for fire-and-forget work like publishing events or independent side effects:
```go
type Job struct{ ID int }
jobs := []Job{{1}, {2}, {3}}
lop.ForEach(jobs, func(j Job, _ int) {
// process each job concurrently
// send(j)
})
```