3 ms·
This is a very common newbie mistake. Fix by passing in i. for i := 0; i < 10; i++ { go func(i int) { f(i) // sees whatever value i has wh
by mykowebhn 6y ago
This is a very common newbie mistake. Fix by passing in i.
for i := 0; i < 10; i++ {
go func(i int) {
f(i) // sees whatever value i has when f() is called, usually 10
}(i)
}
- thereare5lights 6y agoThis looks very much like the mistake in javascript with callbacks in for loops.
- panopticon 6y agoNote that using `let` instead of `var` in your for-loop fixes this in JavaScript (ignoring IE's faulty implemention of `let`).
- deleted 6y ago[deleted]
- rmrfrmrf 6y agoabsolutely wild to me that Real Engineers (TM) complained about this in JavaScript for years only for it to be called a "newbie mistake" in Go.
- shawnz 6y agoThe same is true of most of JavaScript's warts