Channels
Unbuffered Channels
Operation | Nil channel (var ch chan T) | Open channel (make(chan T )`) | Closed channel (close(ch)) |
---|---|---|---|
Send (ch <- v ) | Blocks forever - deadlock panic if no other goroutines | Blocks until a receiver is ready | Panic: send on closed channel |
Receive (<-ch ) | Blocks forever - deadlock panic if no other goroutines | Blocks until a sender is ready | Returns zero value immediately. ok=false |
Close (close(ch) ) | Panic: close of nil channel | Succeeds (only once!) | Panic: close of closed channel |
Range (for v := range ch ) | Blocks forever - deadlock panic | Iterates until channel closed | Iterates until drained. Then exits |
Buffered Channels
Operation | Nil 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 goroutines | Succeeds if buffer not full. Blocks if full until space | Panic: send on closed channel |
Receive (<-ch ) | Blocks forever - deadlock panic if no other goroutines | Succeeds if buffer not empty. Blocks if empty until sender arrives | Returns zero value immediately. ok=false |
Close (close(ch) ) | Panic: close of nil channel | Succeeds (only once!) | Panic: close of closed channel |
Range (for v := range ch ) | Blocks forever - deadlock panic | Iterates until channel closed and buffer drained | Iterates 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.