Lesson 38 of 38 · Concurrency
Boss Battle: The Goroutine Hydra (Go Concurrency Challenge)
The Goroutine Hydra blocks the way. Every test you pass deals damage — pass them all to defeat it and claim a loot chest.
The Goroutine Hydra has crawled out of the sea onto the shores of the Gopher Isles. Every time you cut off one head, two more grow back — no single gopher can keep up. Only perfectly coordinated goroutines can bring it down: a disciplined pool of fighters, one merged stream of battle reports and a scoreboard that never loses a point.
Battle plan
This final challenge combines the big patterns of the chapter.
Worker pool. Instead of one goroutine per job, start a fixed number of workers that all receive from the same jobs channel. Every value sent on a channel is delivered to exactly one receiver, so each job is handled exactly once. When the jobs run out, close the channel: every worker's range loop ends, and a sync.WaitGroup tells you when the last worker is done. To keep the results in input order, send the job's index and let the worker write to results[i].
Fan-in. To merge several channels into one, start one goroutine per input that forwards its values to a shared output channel. The output may only be closed after all forwarders have finished — close it earlier and the remaining senders panic with send on closed channel. One extra goroutine does exactly that:
go func() {
wg.Wait() // every forwarder is done
close(out) // nobody sends on out anymore
}()
Shared state behind a mutex. Many heroes report hits at the same moment, so the scoreboard's map is protected by a sync.Mutex. Lock it in every method — reads included — and unlock with defer. A method that holds the lock must not call another method that locks it again: Go's mutex is not re-entrant.
Assignment
1. ProcessAll(jobs []int, workers int, f func(int) int) []int is a worker pool:
- Start
workersgoroutines (use 1 ifworkersis less than 1) that take jobs from a channel and applyfto them. - Return the results in input order:
result[i] == f(jobs[i]). Callfexactly once per job. No jobs give an empty result. - At most
workerscalls offmay run at the same time.
2. FanIn(chans ...<-chan int) <-chan int merges channels:
- It returns a channel that delivers every value from every input, in any order, and is closed once all inputs are closed. Without inputs, the returned channel is closed right away.
- It returns immediately; the forwarding happens in goroutines.
3. Scoreboard keeps every player's total. Keep the mu field from the starter (one test locks s.mu to check that your methods wait for it). All methods must be safe for concurrent use:
NewScoreboard() *Scoreboardreturns an empty, ready-to-use scoreboard.Add(player string, points int)addspoints(possibly negative) to the player's total.Score(player string) intreturns the player's total,0for unknown players.Top() (string, int)returns the player with the highest total and that total. On a tie, the alphabetically first name wins. Totals can be negative: a board with onlyAdd("rex", -3)returns"rex", -3. An empty board returns"", 0.
ProcessAll([]int{1, 2, 3}, 2, func(x int) int { return x * 10 }) // [10 20 30]
board := NewScoreboard()
board.Add("zoe", 10)
board.Add("max", 10)
board.Top() // "max", 10Premium lesson
Unlock the full Learn Go course
- All 38 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Keep learning
Need a quick reminder? The Go cheat sheet has every topic of Learn Go on one page.
Lesson last updated on .