Usage
To wait for multiple goroutines to finish, we can use a wait group.
This is the function we’ll run in every goroutine. Note that a WaitGroup must be passed to functions by pointer.
On return, notify the WaitGroup that we’re done.
package main
import (
"fmt"
"sync"
"time"
)
func worker(id int, wg *sync.WaitGroup) {
defer wg.Done()
fmt.Printf("Worker %d starting\n", id)
time.Sleep(time.Second)
fmt.Printf("Worker %d done\n", id)
}
func main() {
var wg sync.WaitGroup
for i := 1; i <= 5; i++ {
wg.Add(1)
// Launch several goroutines and increment the WaitGroup counter for each.
go worker(i, &wg)
}
// Block until the WaitGroup counter goes back to 0; all the workers notified they’re done.
wg.Wait()
}