Table of Contents
Month 3 · Week 1 — Exercises¶
Practice for goroutines, channels, and the core concurrency patterns. Each
folder is its own package with a solution and table-driven tests. Standard
library only. Run everything under the race detector — concurrency bugs hide
from plain go test.
| Exercise | Concept | Day | Run tests |
|---|---|---|---|
fanin/ |
fan-in Merge, sync.WaitGroup, close-once |
062 | go test -race ./fanin |
workerpool/ |
bounded concurrency, ordered results, range over jobs |
057–058 | go test -race ./workerpool |
pipeline/ |
generator → map → filter → sink stages | 062 | go test -race ./pipeline |
How to use¶
- Try it yourself first — rename the solution aside and re-implement from the prompt below.
- Run
go test -race ./...from this directory. - Compare with the provided solution; log differences in your day note's "Mistakes" section.
Prompts¶
- fanin: implement
Merge(cs ...<-chan int) <-chan intthat forwards every value from every input channel onto one output channel and closes it exactly once, after all inputs are drained. Use one goroutine per input plus async.WaitGroupand a closer goroutine. Order across inputs is unspecified. - workerpool: implement
Map(inputs []int, workers int, fn func(int) int) []intthat processes inputs with a fixed pool ofworkersgoroutines pulling from a jobs channel, returning results in input order. Tag each job with its index so workers write into a pre-sized slice without locking. Clampworkersto ≥ 1. - pipeline: implement
Gen,Map,Filter, andCollectstages where each stage owns a goroutine and returns a channel it closes on completion. Compose them so a single FIFO path preserves order.
Results¶
| Exercise | Tests | Status |
|---|---|---|
| fanin | TestMerge, TestSum |
✅ |
| workerpool | TestMapPreservesOrder, TestMapAppliesFnOncePerInput |
✅ |
| pipeline | TestPipeline, TestStages |
✅ |
Tip: run all of Week 1 at once with
go test -race ./....