Update waitgroups.go for safe variable usage
Correctly handle passing variables in a loop to a goroutine.
This commit is contained in:
parent
43825687bb
commit
c235416b73
@ -32,6 +32,8 @@ func main() {
|
|||||||
// Launch several goroutines and increment the WaitGroup
|
// Launch several goroutines and increment the WaitGroup
|
||||||
// counter for each.
|
// counter for each.
|
||||||
for i := 1; i <= 5; i++ {
|
for i := 1; i <= 5; i++ {
|
||||||
|
// https://golang.org/doc/faq#closures_and_goroutines
|
||||||
|
i := i
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go worker(i, &wg)
|
go worker(i, &wg)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user