Skip to main content

Channels

Unbuffered Channels

OperationNil channel (var ch chan T)Open channel (make(chan T)`)Closed channel (close(ch))
Send (ch <- v)Blocks forever - deadlock panic if no other goroutinesBlocks until a receiver is readyPanic: send on closed channel
Receive (<-ch)Blocks forever - deadlock panic if no other goroutinesBlocks until a sender is readyReturns zero value immediately. ok=false
Close (close(ch))Panic: close of nil channelSucceeds (only once!)Panic: close of closed channel
Range (for v := range ch)Blocks forever - deadlock panicIterates until channel closedIterates until drained. Then exits

Buffered Channels

OperationNil channel (var ch chan T)Open channel (make(chan T, n)`)Closed channel (close(ch))
Send (ch <- v)Blocks forever - deadlock panic if no other goroutinesSucceeds if buffer not full. Blocks if full until spacePanic: send on closed channel
Receive (<-ch)Blocks forever - deadlock panic if no other goroutinesSucceeds if buffer not empty. Blocks if empty until sender arrivesReturns zero value immediately. ok=false
Close (close(ch))Panic: close of nil channelSucceeds (only once!)Panic: close of closed channel
Range (for v := range ch)Blocks forever - deadlock panicIterates until channel closed and buffer drainedIterates until buffer drained. Then exits

Operations Summary

  • Closing: Only sender should close, never receiver. Closing twice panics.
  • Sending: Never send to a closed channel (panic).
  • Nil channel: Any send/receive blocks forever; close panics.
  • Receiving from closed: Safe, returns zero + ok=false.