Go · Chapter 15 of 26

Goroutines

By the end of this chapter you can start goroutines with the go keyword, wait for them with sync.WaitGroup, collect their results in order without two goroutines touching the same variable, say what a goroutine costs, and explain why a program can end before its goroutines do. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

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

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

Why you need this

Chapter 1 ended with a promise. lab heartbeats recorded 1,180 heartbeats one at a time in 6.77 s, and all at once in a few milliseconds, with one goroutine each. This is the chapter where you write that.

BijliGo's central system is slow the way every busy system across a network is slow: asking it for one charger's status takes 40 ms, and recording one heartbeat takes 5 ms. Tenzin's dashboard shows a station's chargers; the evening review asks about hundreds. One at a time, 50 chargers take 2 seconds, and every one of those seconds is spent waiting, not working. Everything you know so far runs one statement after another, so the second call cannot start until the first one has come back. Go lets you start the calls side by side, wait for all of them together, and pay for the slowest one instead of the sum.

What you build: 3 tasks

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

  1. Task 1

    statuses.go: the status of many chargers at once

    Write Statuses(ctx context.Context, c *bijli.Central, ids []string) []bijli.Status. It asks the central system (c.ChargerStatus, passing ctx on as it is) for every charger in ids at the same time, one goroutine each, waits for all of them, and returns the statuses in the same order as ids: element i is the status of ids[i].

    One call takes 40 ms. The Check asks for 50 chargers (C1150, then C1102 to C1149, then C1101): at once they must take well under a second, with at least 10 calls running at the same moment, and exactly one call each. When a charger's call fails (the Check makes C1003 not answer), its element is a zero bijli.Status. For no ids, Statuses returns an empty slice. The Check runs under the race detector.

  2. Task 2

    available.go: how many chargers are available, counted at once

    Write CountAvailable(ctx context.Context, c *bijli.Central, ids []string) int. It asks for every charger in ids at the same time, one goroutine each, and returns how many have the State available. A charger whose call fails (it is not answering) is not available.

    For C1001 to C1060 it returns 55 (C1019 and C1057 are faulted, C1023, C1037 and C1043 offline), well under a second with at least 10 calls at once. With C1001 and C1002 not answering it returns 53 (the Check tries that three times). The Check runs under the race detector: two goroutines must never change the same variable at the same time.

  3. Task 3 · interview question

    Interview: heartbeats.go, the heartbeats that vanished

    Tenzin's RecordAll(ctx context.Context, c *bijli.Central, ids []string, at time.Time) starts one goroutine for each charger to record its heartbeat (bijli.Heartbeat{Charger: id, At: at}) and returns in a millisecond, before any heartbeat is recorded. Fix it so that when it returns, every charger in ids has exactly one heartbeat recorded, still all at once.

    The Check records all 1180 chargers with heartbeats of 50 ms each: it looks at the last charger, C2180, the moment RecordAll returns, then at all of them; the whole call must take well under a second, with at least 10 calls at once. It runs under the race detector, and fails if Add is called inside the new goroutine.

    Joseph will ask: why did the program end before its goroutines did?

What you will learn

  • The go keyword: start a function running alongside the rest.
  • main does not wait: when main returns, the program ends, goroutines and all.
  • sync.WaitGroup: Add, Done and Wait, and wg.Go (Go 1.25).
  • Results by index: a slice made first, each goroutine writing its own element.
  • One variable per loop turn: why each goroutine sees its own id (Go 1.22).
  • What goroutines cost: 1,180 of them, and 100,000.
  • Two goroutines, one variable: the data race, and the race detector.

More in Concurrency

Build "Goroutines" yourself

BijliGo, a Bengaluru EV charging network, drops chargers at the evening peak. You rewrite its backend in Go. 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 are free. This one comes with the all labs plan.