Wait Groups

A WaitGroup is used to wait for a collection of Goroutines to finish executing. Control is blocked until all Goroutines have finished running.


var wg sync.WaitGroup

// Increment the WaitGroup counter
// to 1, to wait for the goroutine that
// will be called next.
wg.Add(1)

go func() {
    fmt.Println("Go1")
    wg.Done()
}()

// Lock current goroutine flow (main function)
// until the waitGroup counter is zero.
wg.Wait()
                        
wait_groups.go