While working with concurrently running code branches, it is no exception that at some point the program needs to wait for concurrently running parts of the code. This recipe gives insight into how to use the WaitGroup to wait for running goroutines.
Synchronizing goroutines with WaitGroup
How to do it...
- Open the console and create the folder chapter10/recipe05.
- Navigate to the directory.
- Create the file syncgroup.go with the following content:
package main
import "sync"
import "fmt"
func main() {
wg := &sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(1)
go func(idx int) {
// Do some work
defer wg...