All goroutines are asleep while using WaitGroup

I’m exploring how to use WaitGroup to wait for all goroutines to finish. Here is my code

package main

import (

func main() {
	var wg sync.WaitGroup

	go one(wg)
	go two(wg)
	go three(wg)


func one(wg sync.WaitGroup) {
	defer wg.Done()
	time.Sleep(5 * time.Second)

func two(wg sync.WaitGroup) {
	defer wg.Done()
	time.Sleep(7 * time.Second)

func three(wg sync.WaitGroup) {
	defer wg.Done()
	time.Sleep(3 * time.Second)

I keep getting this error “all goroutines are asleep - deadlock” after all the goroutines have printed. I’m unable to understand what’s wrong here - there are no channels involved - it’s just a simple flow. What am I missing?

You are copying the WaitGroup by value, you need to pass it by reference, using wg := new(sync.WaitGroup) is idiomatic.


Thank you.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.