An unbuffered channel (make(chan T)) hands a value directly from a sender to a receiver, so both goroutines must be ready at the same moment. A buffered channel (make(chan T, n)) has room for n values, so a send only blocks when the buffer is full and a receive only blocks when it is empty. Use unbuffered channels for synchronisation and handoff, and buffered channels to absorb bursts or limit concurrency.
The choice changes when goroutines block, so it affects correctness as well as performance.
On an unbuffered channel a send completes only when another goroutine receives, and the receive completes only when a sender is present. The Go memory model guarantees that a send happens before the corresponding receive completes, so the pair is a synchronisation point.
The main goroutine waits on <-done, so reading data afterwards is race free. A chan struct{} carries no data and is the idiomatic type for pure signals.
A buffered channel is a fixed-size FIFO queue. Sends succeed immediately until the buffer holds n values.
len(ch) is the number of queued values and cap(ch) is the capacity. Both are snapshots, so do not make decisions from them in concurrent code.
| Property | Unbuffered | Buffered |
|---|---|---|
| Declaration | make(chan T) | make(chan T, n) |
| Send blocks when | No receiver is ready | Buffer is full |
| Receive blocks when | No sender is ready | Buffer is empty |
| Guarantees handoff | Yes, sender knows receiver has it | No, only that it is queued |
| Typical use | Signals, ping-pong, strict ordering | Work queues, bursts, semaphores |
| Deadlock risk | Higher if both sides wait | Lower, but hidden backlog |
A buffered channel lets a producer queue work without waiting for each worker. Closing the channel tells workers to stop once it is drained.
The sum of squares from 1 to 9 is 285. The directional types <-chan int and chan<- int make the intent explicit and let the compiler reject misuse.
A buffered channel of capacity n works as a counting semaphore: acquiring is a send, releasing is a receive.
For many cases errgroup.Group with SetLimit from golang.org/x/sync/errgroup is a ready-made alternative.
ok == false.select to disable a case.In production code prefer context.Context for cancellation, with case <-ctx.Done(): in the select.
Start with unbuffered. Add a buffer only when you can say why: a known burst size, a fixed number of workers, or a semaphore limit. A large buffer does not fix a slow consumer, it only delays the problem and uses memory. Unexplained buffers such as make(chan T, 100) often hide a race or a missing back-pressure design.
The sending goroutine blocks until another goroutine receives a value. If no receiver ever arrives, the program can deadlock, and the runtime reports all goroutines are asleep when every goroutine is blocked.
It can reduce context switches because sends do not wait for receivers, but speed is not the main reason to choose one. Pick by semantics, and benchmark if throughput matters.
No. Close a channel only to signal that no more values will come, for example to end a range loop. Unreferenced channels are garbage collected.
Yes. Each value is delivered to exactly one receiver, which is how worker pools share a jobs channel.
Unbuffered channels synchronise sender and receiver at the moment of transfer, which makes them ideal for signals and strict handoff. Buffered channels add a queue so producers and consumers can run at different speeds, and they double as semaphores. Begin with unbuffered channels, add buffers for a specific reason, close from the sender side, and use select with context for timeouts and cancellation.