A Guide to WaitGroups in Golang. A brief explanation of this powerful… | by Halil Yıldırım | Sep, 2022

A brief explanation of this powerful feature

Photo by Semyon Borisov on Unsplash

Goroutines are great tools to use, but there is a problem with them. In this piece, we will investigate the problem and we will see how WaitGroups can help us solve this issue.

We will write code to crawl websites and log the status code of them.

When you run the code below, each site will be crawled one by one. In other words, to crawl, the code needs to wait until is finished being crawled. It is not optimal code. We will leverage goroutines to make this code faster.

We can make the code faster using goroutines. Each site will be crawled asynchronously so the code doesn’t have to wait for to be crawled to crawl

When you run the code below, you will create four new goroutines. But when you run this code, you won’t see any output because between 15–17 lines you create the goroutines. Then the main function exits so the process is destroyed, and thus all your goroutines are destroyed before they finish their job because the main goroutine doesn’t know it needs to wait.

A waitgroup waits for goroutines to finish. Let’s inspect the code line by line to see what’s happening.

  • Line 17 — we say we have four goroutines to wait, so waitgroup will increment the counter by 4. (We say four goroutines because we already know we have four websites to crawl and we will create one goroutine for each.)
  • Line 19 — We start one new goroutine for each website to crawl.
  • Line 20 — We give feedback to our waitgroup that the goroutine is done. So it decrements the counter by one.
  • Line 24 — this line blocks the main goroutine until each of the four goroutines say they are done. It blocks until the counter will become zero, then the main function exists.

If counter doesn’t become to zero, wg.Wait blocks will go on forever.
If counter becomes negative number, it panics.

If you want to implement the code like below, you need to pass waitgroup by pointer. Otherwise, it will copy the instance we created on line 10 so when we say wg.Done on line 25. It won’t decrement the counter of the instance wg.Wait wait for. So the counter of the instance on line 10 will remain 4 so wg.Wait will block it forever, and the counter remains 4.

Thank you for reading. Stay tuned for more.

News Credit

%d bloggers like this: