Goroutines and WaitGroups in Go: Concurrency

Narayan Shrestha
readytowork, Inc.
Published in
2 min readMar 23, 2023

Go is a programming language that is designed to make concurrent programming easy and efficient. One of the key features of Go’s concurrency model is Goroutines and WaitGroups. In this article, we’ll explore what Goroutines and WaitGroups are, how they work, and how to use them effectively in your Go programs.

Goroutines

In Go, a Goroutine is a lightweight thread of execution that can be created using the go keyword. Goroutines are managed by the Go runtime, which handles scheduling and communication between Goroutines. Because Goroutines are lightweight, it's possible to create thousands or even millions of Goroutines in a single program without overwhelming the system.

Here’s an example of how to create a Goroutine in Go

func worker() {
// do some logic
}
func main() {
go worker()
// do other work
}

In this example, the worker() function is executed as a Goroutine using the go keyword. The main function continues to execute concurrently with the Goroutine.

Goroutines makes it easy to execute code concurrently, which can improve the performance of your Go programs. For example, if you have a program that needs to perform a large number of I/O operations, you can use Goroutines to execute those operations concurrently, which can improve the overall performance of the program.

WaitGroups

In Go, a WaitGroup is a synchronization mechanism that can be used to wait for multiple Goroutines to finish before continuing execution. A WaitGroup maintains a count of active Goroutines, and the Wait() method blocks until the count reaches zero.

Here’s an example of how to use a WaitGroup in Go

func worker(id int, wg *sync.WaitGroup) {
defer wg.Done()
// do some code
}
func main() {
var wg sync.WaitGroup
for i := 1; i <= 10; i++ {
wg.Add(1)
go worker(i, &wg)
}
wg.Wait()
fmt.Println("All workers done")
}

In this example, a WaitGroup is created using the sync.WaitGroup type. The worker() function is executed as a Goroutine, and WaitGroup's Add() method is called to increment the count of active Goroutines. When the worker() function is finished, the WaitGroup's Done() method is called to decrement the count. Finally, the Wait() method is called to block until the count reaches zero.

WaitGroups are useful when you need to wait for multiple Goroutines to finish before continuing execution. For example, if you have a program that needs to execute a large number of tasks concurrently, you can use a WaitGroup to wait for all the tasks to finish before continuing execution.

Conclusion

Goroutines and WaitGroups are powerful features of Go’s concurrency model that make it easy to write efficient and scalable concurrent programs. By using Goroutines to execute code concurrently and WaitGroups to synchronize between Goroutines, you can write programs that take full advantage of the power of modern hardware. With Go’s simple and intuitive concurrency model, concurrent programming is easier and more accessible than ever before.

Happy Coding

--

--