go-concurrency-talk/examples/waitgroup-add-misuse.go
2020-06-05 15:40:50 -04:00

36 lines
477 B
Go

package main
import (
"fmt"
"sync"
)
// START OMIT
func main() {
r := []int{1, 2, 3, 4}
var group sync.WaitGroup
for _, p := range r {
group.Add(1)
go func(p int) {
defer group.Done()
fmt.Println(p)
}(p)
fmt.Println("wait")
group.Wait() // HL
}
fmt.Println("done\n")
for _, p := range r {
group.Add(1)
go func(p int) {
defer group.Done()
fmt.Println(p)
}(p)
}
fmt.Println("wait")
group.Wait() // HL
fmt.Println("done")
}
// END OMIT