Go channels are typed conduits that let one goroutine send values to another, so goroutines communicate and synchronise without sharing memory behind locks. You create one with make(chan T), send with ch <- v, and receive with v := <-ch. An unbuffered channel makes sender and receiver meet; a buffered channel lets the sender run ahead until the buffer is full.
The Go proverb "do not communicate by sharing memory; share memory by communicating" describes the idea. Channels are not a replacement for every mutex, but they are the right tool for handing off ownership of data, signalling events and building pipelines.
A channel has an element type, and every send and receive is checked against it. The zero value of a channel is nil, so always create channels with make.
The send and the receive are a synchronisation point. Everything the sender did before ch <- v is guaranteed to be visible to the receiver after <-ch returns, which is what makes channels safe for passing data between goroutines.
| Kind | Created with | Send blocks when | Receive blocks when |
|---|---|---|---|
| Unbuffered | make(chan int) | no receiver is ready | no sender is ready |
| Buffered | make(chan int, 3) | the buffer is full | the buffer is empty |
Use an unbuffered channel when you want a guaranteed hand-off. Use a buffered channel to absorb short bursts or to limit concurrency, and pick the size deliberately: a buffer does not fix a slow consumer, it only delays the moment the producer blocks.
len(ch) and cap(ch) report the number of queued values and the capacity, but they are only a snapshot and should not drive logic.
The sender closes a channel to say that no more values will come. Receivers can detect this with the two-value receive form or with range.
Rules to remember:
ok set to false.range loop.A function can restrict what it does with a channel by using a directional type. chan<- T is send-only and <-chan T is receive-only. A bidirectional channel converts to either automatically.
This documents intent and lets the compiler reject a receiver that tries to send, or a producer that tries to close its input.
The select statement waits on several channel operations and runs the first one that is ready. It is how you add timeouts, cancellation and non-blocking operations.
In this example the goroutine would block forever on its send after the timeout, which is a goroutine leak. Make results buffered with capacity 1, or have the goroutine also select on ctx.Done(). A default case turns a select into a non-blocking attempt.
A fixed set of workers reading from a shared jobs channel is the most common channel pattern.
Closing jobs ends the workers' loops, sync.WaitGroup tells a helper goroutine when all workers are finished, and that helper closes results so the final range terminates.
fatal error: all goroutines are asleep - deadlock!.context.Context or a done channel.WaitGroup.sync.Mutex or sync/atomic is simpler and faster.for loop no longer share one copy. Older code needs i := i.Run tests with go test -race to catch data races that channels were supposed to prevent.
Use a channel to transfer ownership of data, distribute work or signal events. Use a mutex to protect shared state that many goroutines read and update in place.
It blocks forever, and so does receiving from a nil channel. This is sometimes used deliberately inside select to disable a case by setting its channel to nil.
No. The garbage collector reclaims unused channels. Close a channel only when receivers need to know that no more values will arrive.
Start with unbuffered. Add a buffer when you can explain why, for example a known burst size or a concurrency limit, and measure the result.
Channels let goroutines pass typed values and synchronise at the same time. Unbuffered channels hand data off directly, buffered channels decouple sender and receiver up to their capacity, and close signals the end of a stream. Combine them with select, context and sync.WaitGroup for timeouts, cancellation and worker pools, and keep ownership rules clear so that only the sender closes and no goroutine is left blocked.