WaitGroup

Learn about WaitGroup in the sync package.

Overview of WaitGroup

In lots of previous code, we have encountered the following code lines:

wg.Add()

Or

wg.Wait()

Or

wg.Done()

What exactly do these lines mean? The wg is a variable-holding instance for sync.WaitGroup. Use WaitGroup to wait for all the launched goroutines.

var wg sync.WaitGroup

Requirement of WaitGroup

In order to understand the need for WaitGroup, let’s walk through some simple code.

Get hands-on with 1400+ tech skills courses.