Channels are typed conduits that let goroutines send and receive values safely without sharing memory directly. You create one with make(chan T), send with ch <- v, receive with v := <-ch, and close it with close(ch) when no more values will be sent. An unbuffered channel makes sender and receiver meet; a buffered channel lets the sender continue until the buffer is full.
Go's guideline is to share memory by communicating, not communicate by sharing memory. Channels are the tool for handing off ownership of data, signalling events and building pipelines. For protecting shared state, a mutex is often simpler.
A channel has an element type. Sends and receives block until the other side is ready, which also synchronises the two goroutines.
main blocks on <-ch until the goroutine sends. If no goroutine will ever send, the runtime panics with all goroutines are asleep - deadlock!.
| Kind | Creation | Send blocks when | Typical use |
|---|---|---|---|
| Unbuffered | make(chan int) | No receiver is ready | Hand-off and synchronisation |
| Buffered | make(chan int, 3) | Buffer is full | Smoothing bursts, limiting concurrency |
Do not use a large buffer to hide a slow consumer. It only delays the problem and uses memory.
Only the sender should close a channel, and only once. Closing a closed channel or sending on a closed channel panics. Receivers can detect closure with the second return value, or use range, which stops when the channel is closed and drained.
Closing is a broadcast: every goroutine waiting on the channel is released. That makes close a convenient way to signal completion to many workers at once.
A function can restrict what it does with a channel by using chan<- (send only) or <-chan (receive only). The compiler enforces it, which documents intent.
Returning a receive-only channel from a producer is a common pattern: the caller can only read, and the producer keeps sole ownership of closing.
select waits on multiple channel operations and runs the first one that is ready. Combine it with context.Context and time.After for cancellation and timeouts.
A default case makes a select non-blocking. A nil channel is never ready, which you can use to disable a case dynamically.
A worker pool starts a fixed number of goroutines that read from a shared jobs channel. A sync.WaitGroup tells you when all workers have finished so you can close the results channel.
Since Go 1.22, each loop iteration has its own variable, so closures started in a for loop no longer share a single loop variable. Go 1.25 added wg.Go(func() { ... }), which calls Add and Done for you.
sync/atomic would be simpler. A counter protected by sync.Mutex is clearer than a channel-based one.Run tests with go test -race to catch data races, and use go vet to find some misuse.
You receive the zero value immediately, with ok set to false once all buffered values have been read.
No. A second close panics. Make one goroutine responsible for closing.
Use a mutex to protect shared state that many goroutines read or update. Use a channel to transfer ownership of data or to signal events.
Use select with time.After or, better, a context.WithTimeout and its Done channel.
Channels give goroutines a safe way to exchange values and synchronise. Use unbuffered channels for hand-offs, buffered ones for bounded queues, select for multiplexing and cancellation, and let the sender close. Avoid leaks by always giving goroutines an exit path.