Buffered Collect
The pipeline lesson’s channel was unbuffered: every send blocked until a
receiver arrived. Sender and collector moved in lockstep, each paced by the
other. Sometimes that’s exactly the backpressure you want. Here it isn’t —
n workers shouldn’t queue behind one drain. This exercise gives the
channel a waiting room.
Unbuffered is a handshake
ch := make(chan int) // capacity 0: send waits for receive, and vice versaEvery value changes hands directly, sender to receiver, in the same instant. Elegant — and coupled: a fast worker idles until the collector asks, a slow collector stalls every worker behind the current one. For one-to-one rhythms that’s fine. For fan-out, it’s a traffic jam with extra steps.
Buffered is a queue
ch := make(chan int, n) // capacity n: up to n sends complete immediatelyA buffered channel holds up to n values without any receiver
present. Sends proceed until the buffer fills; receives proceed while it’s
non-empty. Workers deposit results and exit; the collector drains at
leisure. Nobody waits on anybody — the pace decouples in both directions.
The sizing rule for this pattern: buffer for every worker. n
goroutines each send once, so capacity n guarantees no sender ever
blocks:
func Collect(n int) []int {
type result struct{ i, v int }
ch := make(chan result, n)
for i := 0; i < n; i++ {
go func() {
ch <- result{i, 2 * i}
}()
}
out := make([]int, n)
for range n {
r := <-ch
out[r.i] = r.v
}
return out
}Size the buffer to the sends and the deadlock question answers itself: every send has a slot, every slot gets drained, the function always returns. Undersize it and workers park mid-send — with no receiver running yet (collection happens after launching), the program would hang forever. When a channel program deadlocks, count sends against buffer plus receivers first; the arithmetic is almost always the bug.
The index tags do the same duty as the pipeline lesson: arrival order is
scheduler chaos, out[r.i] restores input order. Same trick, new reason
to need it — buffered collection scrambles even harder, since nothing
paces the workers at all.
sequenceDiagram
participant W as Workers x n
participant B as Buffer cap n
participant C as Collector
W->>B: send indexed results
Note over W,B: no worker ever blocks - a slot always waits
C->>B: drain n receives
C->>C: place by index into ordered output
Hint
Size the buffer to the worker count — make(chan result, n) — so no send
ever blocks. Then receive exactly n times, placing each by its index
tag.
Task
Complete Collect in collect.go so n goroutines each send one doubled
index over a buffered channel and results land in order:
func Collect(n int) []int {
// ...
}Check
go test -race ./exercises/07_concurrency/03_buffered/ -vThe -race flag is required here: n goroutines writing through one
channel is exactly the shape the detector was built to verify.
Source: exercises/07_concurrency/03_buffered/README.md · Inspired by Mainmatter’s 100 Exercises to Learn Rust (CC BY-NC 4.0) — all Go prose here is original, free for non-commercial use.