`sync.WaitGroup` is a counter-based synchronization primitive used to wait for a set of goroutines to finish.
package main import ( "fmt" "sync" ) func main() { var wg sync.WaitGroup for i := 0; i < 3; i++ { wg.Add(1) go func(id int) { defer wg.Done() fmt.Println("worker:", id) }(i) } wg.Wait() fmt.Println("all done") }