Performing async operations with sync.WaitGroup
Sometimes, it is useful to perform a number of operations asynchronously, then wait till they complete before moving on. For example, if an operation requires pulling information from multiple APIs and aggregate that information, it can be helpful to make those client requests asynchronously. This chapter will explore using sync.WaitGroup
to orchestrate non-dependent tasks in parallel.
Getting ready
Refer to the Getting ready section of the Using channels and the select statement recipe in this chapter.
How to do it...
These steps cover writing and running your application:
- From your terminal/console application, create the
chapter9/waitgroup
directory and navigate to it. - Copy tests from https://github.com/agtorre/go-cookbook/tree/master/chapter9/waitgroup or use this as an exercise to write some of your own code.
- Create a file called
tasks.go
with the following content:
package waitgroup import ( "fmt" "log...