Category: Data

Cryptography, Programming, and Security Articles For example, if I have a web page that needs to request 7 files from the server before it can show the page, I need to asynchronously fetch all those files at the same time.

the problem is that after starting 3 separate goroutines and yielding execution back to the main thread, there is nothing stopping main() from exiting.

In order to make sure that we wait for all of our functions to complete, but to still allow them to execute at the same time, we use a WaitGroup.

We pass a pointer to the WaitGroup to each goroutine and use the defer keyword to mark a counter done when each goroutine exits.

Related Articles