You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When doing anonymous function inside a for loop, you might see a warning range variable i captured by func literal.
fori, e:=rangeitemIDs {
// This work as expectedii, ee:=i, egofunc() {
deferwg.Done()
iferr:=GetStoryByID(ee, &items[ii]); err!=nil {
log.Fatalf("Error: %s", err)
}
}()
// This WONT work as expectedgofunc() {
deferwg.Done()
iferr:=GetStoryByID(e, &items[i]); err!=nil {
log.Fatalf("Error: %s", err)
}
}()
}
With our go func().. statement we start a new goroutine. They run concurrently, which they do not run one after the other in an orderly fashion. They could in theory run one after the other, or they could all run at the same time (in parallel). Or maybe the "last one" runs first and then the "first one" and the "second one" runs last, or maybe.. think you get the point; it's unpredictable.
When doing anonymous function inside a for loop, you might see a warning
range variable i captured by func literal
.With our
go func()..
statement we start a new goroutine. They run concurrently, which they do not run one after the other in an orderly fashion. They could in theory run one after the other, or they could all run at the same time (in parallel). Or maybe the "last one" runs first and then the "first one" and the "second one" runs last, or maybe.. think you get the point; it's unpredictable.Ref: http://oyvindsk.com/writing/common-golang-mistakes-1
The text was updated successfully, but these errors were encountered: