Skip to content

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

  1. Try it yourself first — rename the solution aside and re-implement from the prompt below.
  2. Run go test -race ./... from this directory.
  3. Compare with the provided solution; log differences in your day note's "Mistakes" section.

Prompts

  • fanin: implement Merge(cs ...<-chan int) <-chan int that 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 a sync.WaitGroup and a closer goroutine. Order across inputs is unspecified.
  • workerpool: implement Map(inputs []int, workers int, fn func(int) int) []int that processes inputs with a fixed pool of workers goroutines 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. Clamp workers to ≥ 1.
  • pipeline: implement Gen, Map, Filter, and Collect stages 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 ./....


Exercises