Channel capacity is the amount of message-holding space available before a send must wait for receiving capacity. Treating that capacity as a mutually exclusive resource makes channel deadlocks easier to reason about. section 11.3
A deadlock appears when goroutines hold channel resources while requesting others, creating a circular wait. With unbuffered channels, sending and receiving must coordinate directly, so two channels can block each handler while it waits on the other. section 11.3 section 11.5
Recursive directory traversal
One goroutine reads directories and sends paths through a files channel; another processes files and sends discovered directories through a dirs channel. The program prints several files, then can stop when both handlers wait on the channels needed by the other. The example uses a 60-second sleep rather than structured completion, making the blocked state observable.
Avoid circular waits by changing how channel capacity is consumed:
- Redesign message flows when separate goroutines or select only disguise a circular dependency. section 11.5
