By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A channel is a typed conduit for passing values between goroutines. An unbuffered channel synchronizes sender and receiver: the send completes only when a receiver takes the value. A buffered channel decouples them by up to the buffer size, which smooths bursts and lets a producer finish without waiting. The zero value is a nil channel, and a send or receive on it blocks forever — useful as a deliberate 'off switch' in a select.
Direction types document intent: chan string is bidirectional, chan<- string is send-only, <-chan string is receive-only. A producer returns <-chan int so callers cannot close or write it; the producer keeps the writable end. Only the sender should close a channel, and only when no more values will arrive. Closing is a broadcast: every receiver unblocks immediately.
select waits on several channel operations at once. If more than one is ready, it picks one at random; if none is ready and there is a default, the default runs. That makes select the tool for timeouts, cancellation and non-blocking drains. The canonical cancellation case is case <-ctx.Done(): return; the canonical drain is select { case v := <-ch: ... default: }.
package main
import "fmt"
func produce(done <-chan struct{}) <-chan int {
out := make(chan int, 3)
go func() {
defer close(out)
for _, status := range []int{200, 404, 500} {
select {
case out <- status:
case <-done:
return
}
}
}()
return out
}
func main() {
done := make(chan struct{})
for status := range produce(done) {
fmt.Println("received", status)
}
close(done)
select {
case <-done:
fmt.Println("done closed")
default:
fmt.Println("unexpected")
}
queue := make(chan string, 1)
queue <- "event"
select {
case next := <-queue:
fmt.Println("drained", next)
default:
fmt.Println("empty")
}
}Most channel code is one of four shapes: a producer that closes its output, a consumer that ranges until close, a select that mixes work with ctx.Done(), and a buffered queue that absorbs bursts. Pipelines chain the first three: each stage ranges over its input channel, transforms values, and closes its own output channel when the input ends. Fan-in merges several channels with a WaitGroup and one output channel — step 22 builds exactly that.
| Pattern | Shape | Use it for |
|---|---|---|
| Unbuffered handoff | ch := make(chan T) | Synchronization and backpressure |
| Buffered queue | make(chan T, 100) | Absorbing short bursts |
| Producer closes | defer close(out) | Telling consumers no more values |
| Range consumer | for v := range ch | Drain until close |
| Cancellation | case <-ctx.Done() | Abandoning work safely |
| Non-blocking check | select { ... default: } | Try, then move on |
Key takeaways
Channels transfer values between goroutines; unbuffered sends synchronize, buffered sends decouple up to the buffer size.
Use directional channel types and return the receive-only end from producers.
Only the sender closes a channel, and only when no more values will arrive.
select multiplexes channel operations and is how context cancellation joins ordinary work.
Range over a channel until it is closed; a nil channel blocks forever.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.