What is WaitGroup in Golang?

Learn via video courses
Topics Covered

Overview

Golang supports concurrent programming by default. This is very powerful but writing code is tricky and causes a problem. In this article, we will learn more about goroutines and waitgroup in Golang.

What are Goroutines in Golang?

Concurrency is when two or more function runs simultaneously and are independent of each other. Golang achieves concurrent programming by employing Goroutines. Go-runtime manages all the Goroutines which run as a lightweight thread. Every Golang program has a minimum of one Goroutine and that Goroutine is known as the main. All the Goroutines are working under the main Goroutines if the main Goroutine is killed, then all the Goroutines in the program are also terminated.

How to Do Goroutines Work?

Goroutines allow our application to become asynchronous in nature. Goroutines are different from threads and are managed by Go runtime.

The Go runtime scheduler takes control of the lifecycle and allocates the Goroutine with an OS thread and memory when several Goroutines are executing in a Golang program.

A Goroutine does a context switch when it blocks. It is the process of saving the state of the goroutine so that it can be restored and executed later. So while one goroutine is stored in the background, the Go scheduler gives the thread to another goroutine for execution

How Goroutines work

Problems with Goroutines in Golang and Ways to Solve Them

Let us understand the problem that occurs due to goroutines in golang.

This is because as soon as you launch both the goroutines, your main function just got terminated. And every program in Golang executes until the main function is not terminated.

The output of the program is mostly empty because as soon as we launch the goroutines the main function gets terminated. This is because every goroutine in Golang's terminates as soon as the main function execution is completed.

Example:

Output:

Try On Playground

The 2 go-routines was unable to complete their execution and terminated the moment the main routine stopped.

The problem can be handled in two ways;

  • Using time package to add delay in main functions.
  • Implementing a waitgroup in golang.

Using Time Package

By introducing a delay in the program, it gives goroutines time to complete the execution.

Example:

Output:

Try On Playground

The execution of goroutines can be random. Using the time.Sleep program is waiting for 2 seconds. This gives enough time to complete the other goroutines. This workaround is not the ideal way to handle go-routines. What if other goroutines take more time than 2 seconds. Then in that scenario it will end its execution at the end of the main goroutine.

To handle this better we have Waitgroup.

What are the Waitgroups in Golang?

Golang Waitgroup allows you to block a specific code block to allow a set of goroutines to complete execution. An example would be to block the main function until the goroutines are completed and then unblocks the group.

Waitgroup is available as a standard package and can be imported from the sync package.

Methods of Waitgroups in Golang

  • Add - The Waitgroup acts as a counter holding the number of functions or go routines to wait for. When the counter becomes 0 the Waitgroup releases the goroutines. 
  • Wait - The wait method blocks the execution of the application until the Waitgroup counter becomes 0.
  • Done - Decreases the Waitgroup counter by a value of 1

How Do Waitgroups Solve Concurrency Problems in Golang?

The below example shows how we can solve the concurrency problem using Waitgroup in Golang.

You can use WaitGroup to solve the problem of empty output caused by goroutines. To use this package, import sync.WaitGroup from the standard Golang packages.

The Waitgroup acts like a counter and blocks the execution of the program until this counter becomes 0.

Output:

Try on Playground

Conclusion

  • Explained the concepts of Waitgroup in Golang.
  • Understood the problem with Goroutines.
  • Implemented examples for Waitgroup in Golang.