How to: Handle Errors within Wait Groups
·
292 words
·
2 minutes read
One of the many benefits of using Go is it’s simplicity when it comes to concurrency. With WaitGroups being an excellent example of this. It can be tricky though to handle both concurrency and errors effectively. This post aims to outline how you can run multiple goroutines and handle any errors effectively, without stopping program execution.
The essence of this comes down to these three key parts:
- Creating two channels, for passing errors and when the WaitGroup is complete.
- A final goroutine to listen for the WaitGroup to complete, closing a channel when that happens.
- A
select
listening for errors or the WaitGroup to complete, whichever occurs first.
|
|