A nil channel in Go is a channel variable that was never initialised with make. Sending to it blocks forever, receiving from it blocks forever, and closing it panics. Inside a select statement, a case on a nil channel is never ready, which makes a Go nil channel a useful tool for switching cases off at runtime.
The zero value of every channel type is nil, so var ch chan int gives you one. Most nil channel bugs come from forgetting make, and the symptom is a goroutine that hangs silently.
A channel declared without make has no underlying queue. The language spec defines its behaviour exactly.
| Operation | On a nil channel |
|---|---|
Send ch <- v | Blocks forever |
Receive <-ch | Blocks forever |
close(ch) | Panics: close of nil channel |
len(ch) and cap(ch) | Return 0 |
range ch | Blocks forever |
select case on it | Never selected |
Compare that with a closed channel, where receives return immediately and sends panic. Nil and closed are opposite states, and mixing them up is a frequent source of confusion.
The runtime detects that no goroutine can make progress and aborts. The goroutine status chan send (nil chan) identifies the cause. In a larger program with other goroutines running, there is no such detection, so the stuck goroutine is a leak that you only see in a goroutine dump or a profile.
The fix is to create the channel:
Struct fields are the typical trap. A struct with a chan field has a nil channel until the constructor sets it, so always initialise channels in a New function.
Because a nil channel is never ready in select, you can assign nil to a channel variable to remove that case. This is the standard way to merge two streams until both finish.
Without setting the closed channel to nil, select would keep choosing it, since a receive from a closed channel is always ready, and the loop would spin and send zero values. Setting it to nil turns that case off, and the loop condition a != nil || b != nil ends once both are done.
A struct can hold an optional notification channel. If it is nil, a select with that case simply never fires.
Flip between a real channel and nil to enable or disable a send case, for example to pause producing while a buffer is full.
This is the common pattern for a goroutine that buffers input and writes when the consumer is ready.
kill -QUIT or pprof for chan receive (nil chan) and chan send (nil chan) states.if ch != nil, or better, redesign so the owner always has a real channel.-race flag and tests with timeouts, since a nil channel hang may never trigger the deadlock detector.No. It blocks forever. Only closing a nil channel panics. Sending on a closed channel panics too.
Compare it with nil: if ch == nil. You cannot check whether a channel is closed without receiving.
The detector only fires when every goroutine is blocked. If any goroutine runs, such as an HTTP server loop or a timer, the nil channel goroutine just stays blocked.
Yes. Setting a channel to nil inside a select loop disables its case, which is the idiom for merging streams and for conditional send or receive.
A nil channel blocks forever on send and receive, panics on close, and is never chosen by select. Forgetting make causes silent hangs or deadlock errors, so initialise channels early. Used deliberately, assigning nil to a channel variable turns off a select case, which is how you finish merging several channels cleanly.