Waiting for Goroutines to Finish with a WaitGroup
·
246 words
·
2 minutes read
One of the (many) positives of Go is it’s simple but powerful use of concurrency. By using keywords like go
we’re able to run functions in parallel. As easy as this is, we often need a way to run our next bit of code once all these goroutines have finished. That’s where a WaitGroup
comes in.
A WaitGroup
allows you to specify how many goroutines have been created then wait for them to all be done. This is much more efficient than waiting, for example, 2 seconds for it to complete. In our example below we create 3 goroutines within a loop and wait for them to be complete.
|
|