Level 3: Advanced Go Exercises
Level 3 covers what Go is famous for on the backend: doing many things at once, and speaking JSON. You call services concurrently while keeping the order of results, build a pipeline stage and a worker pool, merge event streams, shape and decode API payloads, chain middleware, and finish with the token-bucket algorithm many APIs use to limit requests.
Concurrency bugs are the hardest to see, so several tests are built to expose them: if your code never closes a channel, runs calls one by one when they should overlap, or closes an output too early, the program stops with a clear message instead of passing by luck.
The 10 exercises in this level
- 1 Goroutines and WaitGroup Call every service at once, keep the order Call several services at the same time with goroutines in Go, wait for all of them with sync.WaitGroup, and return the results in the original order. Start here — no prerequisites
- 2 Channels and close A pipeline stage that keeps only paid orders Build a Go pipeline stage that reads order amounts from a channel, passes on the paid ones and closes its output, so the next stage's range loop can finish. Recommended first: Goroutines and WaitGroup
- 3 Worker Pools Process jobs with a fixed number of workers Build a worker pool in Go: a fixed number of goroutines read jobs from one channel and send results on another, which is closed only after every worker is done. Recommended first: Channels and close
- 4 Select Queue a job, or say the queue is full Use select with a default case in Go to add a job to a bounded queue without blocking, and report a full queue right away instead of freezing the caller. Recommended first: Channels and close
- 5 Fan-In Merge two event streams into one Merge two Go channels into one with the fan-in pattern: one goroutine per input, and the output closed only once both inputs are finished. Recommended first: Channels and close
- 6 JSON and Struct Tags Shape an API response Shape the JSON an API returns with Go struct tags: snake_case key names with json:"...", and omitempty for optional fields, but never on a value that can really be 0. Start here — no prerequisites
- 7 Decoding JSON Total an order from an API body Decode a JSON request body into typed Go structs with json.Unmarshal and total the order, avoiding map[string]any, where every number turns into a float64. Recommended first: JSON and Struct Tags
- 8 Type Switches Describe any decoded JSON value Describe any value decoded from JSON with a Go type switch on an any value, handling null, bools, numbers, strings, lists and objects, where numbers are float64. Recommended first: Decoding JSON
- 9 Middleware Chains Wrap a handler with logging and auth Chain HTTP-style middleware in Go: functions that take a handler and return a wrapped one, applied so the first middleware in the list runs first. Start here — no prerequisites
- 10 Project: Token Bucket Rate-limit an API, one token per request Build, in Go, the token-bucket algorithm many APIs use to limit requests: a bucket that refills at a steady rate, caps at its capacity and spends one token per request. Recommended first: Middleware Chains, Select
Frequently asked questions
- What happens if my goroutines deadlock here?
- Go stops a program whose goroutines are all blocked. The exercise page catches that, tells you it was a deadlock or a panic in a goroutine, and starts a fresh interpreter for your next run.
- Why do JSON numbers become float64?
- When you decode into `any` or `map[string]any`, Go doesn't know which number type you want, so it picks `float64` for all of them. Decode into a struct with typed fields to get `int` values directly.
- How does a token bucket limit requests?
- The bucket holds a few tokens and refills at a steady rate; each request spends one, and an empty bucket means "try later". Short bursts are allowed, but the average rate can't go above the refill rate.