Coordinating Groups of Tasks
Sometimes you need to launch many tasks and wait for all of them to finish before continuing.
go var wg sync.WaitGroup
for i := 0; i < 5; i++ { wg.Add(1) go func(id int) { defer wg.Done() fmt.Printf("Task %d finished\n", id) }(i) }
wg.Wait() // Blocks until all Done() are called