Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Concepts

Channel rules in Go

A complete rule table for Go channels: what send, receive, close and range do on nil, open, closed, buffered and unbuffered channels, with runnable proofs.

A channel's behavior depends on its state: nil, open, or closed, buffered or not, with room or without. Most channel bugs come from not knowing what an operation does in a particular state. The language rules are small and exact, and worth memorizing.

In short: receiving from a closed channel never blocks, sending to a closed channel panics, closing a closed or nil channel panics, and any operation on a nil channel (except close, which panics) blocks forever.

Go
v, ok := <-ch // ok is false once ch is closed and drained

The rule table

Operation nil channel open channel closed channel
ch <- v blocks forever sends, or blocks until a receiver is ready (or buffer space exists) panic: send on closed channel
<-ch blocks forever receives, or blocks until a value arrives buffered values first, then zero value with ok == false
close(ch) panic closes panic: close of closed channel
for v := range ch blocks forever loops until closed drains buffered values, then ends
len(ch), cap(ch) 0, 0 values buffered, capacity remaining buffered values, capacity

Try it

Go Receiving from closed channels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Panics and the nil channel trick

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Usage notes

  • Only the sender closes. Closing is a signal "no more values", not a cleanup. Receivers have no safe way to know whether a sender is still about to send, so the code that owns the sending side closes the channel.
  • With several senders, nobody can close safely without coordination. Use a sync.WaitGroup and let one goroutine close the channel after all senders are done.
  • You do not have to close every channel. Close only when a receiver needs to learn that the stream ended (typically with range). The garbage collector reclaims unreferenced channels.
  • Unbuffered channels synchronize: a send completes only when a receiver takes the value. Buffered channels decouple sender and receiver up to the capacity. A buffer is not a performance fix; it changes which goroutine blocks.
  • Direction types: chan<- T can only send and be closed; <-chan T can only receive. Closing a receive-only channel is a compile error.
  • Select: with several ready cases one is chosen pseudo-randomly. A default case makes it non-blocking. select {} with no cases blocks forever.
  • Deadlock detection: if every goroutine is blocked, the runtime stops the program with fatal error: all goroutines are asleep - deadlock!. A blocked nil-channel operation in one goroutine while others still run is not detected.
  • Zero value signals: a receive from a closed channel yields the zero value; use the two-value form when zero is a legitimate message.