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
This code captures only the last iteration of the for loop. When the goroutines finally run the variable peer has already changed to the last value.
// Asynchronously clear the key from all hot and main caches of peersfor_, peer:=rangeg.peers.GetAll() {
// avoid deleting from owner a second timeifpeer==owner {
continue
}
wg.Add(1)
gofunc() {
errs<-g.removeFromPeer(ctx, peer, key)
wg.Done()
}()
}
./prog.go:16:19: loop variable peer captured by func literal
The solution here is easy though. The smallest working change is to pass the peer as an argument to the goroutine: https://play.golang.org/p/0mNV3nSkGUy
The text was updated successfully, but these errors were encountered:
This code captures only the last iteration of the for loop. When the goroutines finally run the variable
peer
has already changed to the last value.You can see the same efect here: https://play.golang.org/p/I82KNfRkqSP
Go vet will also highlight the error:
The solution here is easy though. The smallest working change is to pass the peer as an argument to the goroutine: https://play.golang.org/p/0mNV3nSkGUy
The text was updated successfully, but these errors were encountered: