WaitGroups
In this lesson, you will learn about the WaitGroups present in the "sync" package which will allow us some control over goroutines.
We'll cover the following
What is a WaitGroup
?
A WaitGroup blocks a program and waits for a set of goroutines to finish before moving to the next steps of execution.
How to use WaitGroup
?
We can use WaitGroups through the following functions:
.Add(int)
: This function takes in an integer value which is essentially the number of goroutines which the waitgroup has to wait for. This function must be called before we execute a goroutine..Done()
: This function is called within the goroutine to signal that the goroutine has successfully executed..Wait()
: This function blocks the program until all the goroutines specified byAdd()
have invokedDone()
from within.
Does this seem like a lot of information right now? Don’t worry, it’ll look easier once we see a sample structure to use the above functions.
Get hands-on with 1400+ tech skills courses.