Go: Wait for goroutines

A sync.WaitGroup waits for a group of goroutines to finish.

var wg sync.WaitGroup
wg.Add(2)

go func() {
	// Do work
	wg.Done()
}()

go func() {
	// Do work
	wg.Done()
}()

wg.Wait()

At the same time, Wait is used to block until these two goroutines have finished.

Note: A WaitGroup must not be copied after first use.

Comments

Be the first to comment!