Go traps · Chapter 12 of 16

WaitGroup.Add inside the goroutine

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Defer
  • 3 tasks
  • Go
  • In the all labs plan

The first 2 chapters of Go traps are free. This one comes with the all labs plan.

Why you need this

The code compiles and go test builds it without a word. go vet knows this trap:

$ go vet .
main.go:15:10: WaitGroup.Add called from inside new goroutine

But go test runs only a few of vet's checks before testing, and this one is not among them. The usual fix is to count before starting: wg.Add(1) on the line before go, or, since Go 1.25, wg.Go(f), which does the Add, the go and the Done for you. With wg.Go:

240
240
240
240

A sleep or runtime.Gosched() before Wait only gives the goroutines a head start. The total is right more often, not always.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.go: a day that adds up to nothing, on purpose

    Write reproduce() in repro.go, which returns an int. Set GOMAXPROCS to 1 with runtime.GOMAXPROCS (and put the old value back before you return), call DayTotal(sessions, cost) from totals.go for a few sessions with a cost function that prices each one above 0 paise, and return what DayTotal returned.

    The check runs your reproduce() twice, each time in a fresh build of the case's folder: beside the original totals.go it must return 0 (the day added up before any session was), and beside a fixed totals.go it must return the day's full total, more than 0. So it must really call DayTotal with sessions that cost something. No real waiting and no input: reproduce() must finish within 3 seconds.

  2. Task 2

    totals.go: the evening total counts every session

    Fix DayTotal(sessions []Session, cost func(Session) int) int in totals.go. It must return the sum of cost(s) for every session, calling cost exactly once a session, and only once every session has been priced and added, however many CPUs the program has; for no sessions it returns 0. Keep the pricing side by side (one goroutine a session), Session, and EveningReport with its line day total <day>: <n> sessions, <paise> paise.

    The check uses its own 200 sessions and its own cost function, and runs DayTotal three times with GOMAXPROCS set to 1 and three times with every CPU, counting the calls to cost. A sleep or a yield before waiting is not a fix (it only makes the short total rarer), and the check fails both.

  3. Task 3

    totals_test.go: a test that would have caught it

    Write a Go test file, totals_test.go, that would have caught this bug before Tuesday's release. The check runs your file twice, each in a fresh build: beside the original totals.go, where at least one test must fail; and beside the fixed totals.go, where every test must pass.

    So call DayTotal for a few sessions whose total you know, with GOMAXPROCS set to 1 for the test (put it back with defer), and fail when the sum is wrong: with every CPU the bug shows up only some of the time. Each test must finish within a second or two (the whole file is stopped after 8 seconds).

What you will learn

  • repro.go: a day that adds up to nothing, on purpose
  • totals.go: the evening total counts every session
  • totals_test.go: a test that would have caught it

More in Defer

Build "WaitGroup.Add inside the goroutine" yourself

BijliGo runs its EV chargers on Go, and Joseph, its senior engineer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Go traps are free. This one comes with the all labs plan.