Update waitgroups.go for safe variable usage

Correctly handle passing variables in a loop to a goroutine.
This commit is contained in:
Sean Lydon 2019-11-02 12:57:14 -07:00 committed by GitHub
parent 43825687bb
commit c235416b73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -32,6 +32,8 @@ func main() {
// Launch several goroutines and increment the WaitGroup
// counter for each.
for i := 1; i <= 5; i++ {
// https://golang.org/doc/faq#closures_and_goroutines
i := i
wg.Add(1)
go worker(i, &wg)
}