From the course: Go for Developers: Practical Techniques for Effective Coding
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Wait groups - Go Tutorial
From the course: Go for Developers: Practical Techniques for Effective Coding
Wait groups
- Often we might want to wait for a number of Go routines to finish before we continue our program. For example, we might want to spawn a number of Go routines to create a number of thumbnails of different sizes and then wait for them mall to complete before we continue. So here the generate thumbnail function will generate a thumbnail of a specified size. In this example, we'll sleep for one millisecond per size of the thumbnail. This is to simulate the amount of time it takes to generate a thumbnail. So for example, if we call generate thumbnail with the values foo... For example, if we call generate thumbnail with the values foo.png in 200, it will sleep 200 milliseconds before returning. To test the generate thumbnail function, we launch five new Go routines, each of which will create a new thumbnail of a different size. We then wait for all of them to complete. As we can see when we run our test, the test exits before our thumbnails are generated. Our test exits prematurely…