Skip to content
Does sync.WaitGroup/errgroup.Group wait for all goroutines if wg.Add(1)/eg.Go() is called in a running goroutine?
Go
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore Initial commit Oct 16, 2019
README.md Add sleep before go func()/eg.Go() Oct 16, 2019
go.mod Initial commit Oct 16, 2019
go.sum Initial commit Oct 16, 2019
main.go Add sleep before go func()/eg.Go() Oct 16, 2019

README.md

go-nested-errgroup

RQ

Questions:

  1. Does sync.WaitGroup wait for all goroutines if wg.Add(1) is called in a running goroutine?
  2. Does errgroup.Group wait for all goroutines if eg.Go() is called in a running goroutine?

Answers:

  1. Yes
  2. Yes

Verify

Run main.go.

go run main.go
2019/10/16 10:53:13 starting wg#main
2019/10/16 10:53:13 starting wg#1
2019/10/16 10:53:14 starting wg#2
2019/10/16 10:53:15 finished wg#1
2019/10/16 10:53:17 finished wg#2
2019/10/16 10:53:17 finished wg#main
2019/10/16 10:53:17 starting eg#main
2019/10/16 10:53:17 starting eg#1
2019/10/16 10:53:18 starting eg#2
2019/10/16 10:53:19 finished eg#1
2019/10/16 10:53:21 finished eg#2
2019/10/16 10:53:21 finished eg#main
You can’t perform that action at this time.