-
-
Notifications
You must be signed in to change notification settings - Fork 907
Open
Description
The existing lo.ForEach function makes a copy of every element as it iterates, which is problematic for arrays since that prevents them from being modified.
Example:
type Dummy struct {
f1 string
}
a := []Dummy{{f1: "Hello"}}
fmt.Println(a)
lo.ForEach(a, func(d Dummy, _ int) {
d.f1 += ", world!"
})
fmt.Println(a)
Output:
[{Hello}]
[{Hello}]
To resolve this, I would like to add a method ForEachByRef which would pass a ref to each array element in the function callback. With the new function, the above code would look like:
a := []Dummy{{f1: "Hello"}}
lo.ForEach(a, func(d *Dummy, _ int) {
// etc...
If that sounds fine, can I raise a PR to add this function?
DreamerKMP
Metadata
Metadata
Assignees
Labels
No labels