Fix Go "panic: close of nil channel" — the Unmade Channel, and How Nil Channels Block, Disable Select Cases, and Panic on Close
A nil channel is not a broken channel; it is no channel at all, a variable that was declared but never made. Sends and receives on it block forever, select ignores it, and close panics on it. Below, each behavior gets its own runnable program, then the missing-make shapes that produce the panic in real code, and the fix: make the channel where the value is constructed, and let its owner close it exactly once.
Written November 21, 2026 · Tested with Go 1.26.4 · 17 min read
- The working baseline: make, send, close, range
- The panic itself: close of a channel that was never made
- A send on a nil channel blocks forever
- A receive from a nil channel blocks forever
What this guide is
You hit panic: close of nil channel when code calls close on a channel variable that holds nil — a channel that was declared but never created with make. Declaring var ch chan int gives you the variable and its zero value, which for a channel type is nil. Nothing about that declaration allocates a channel, and no send, receive, or close on the nil value can ever do useful work.
The nil value has three behaviors, and keeping them separate is the whole diagnosis. A send on a nil channel blocks forever. A receive from a nil channel blocks forever. close is the exception: instead of blocking, closing a nil channel panics, which is why the missing make so often surfaces at the close call rather than at the declaration. The specification states the first two directly — a send on a nil channel blocks forever, and receiving from a nil channel blocks forever — and it states the underlying reason in one sentence: a nil channel is never ready for communication.
That last sentence also explains the useful side. In a select, a case whose channel is nil can never be chosen, because communication on it can never proceed. Accidentally, that silently disables a case you meant to run. Deliberately, it is the standard way to turn a select case off: assign nil to a channel that is finished, and the select stops considering it. Both sides get a program below.
All examples use Go 1.26.4. Each one is a single main.go in one module. Make the module once, from an empty directory:
$ mkdir nilchannel
$ cd nilchannel
$ go mod init example.com/nilchannel
go: creating new go.mod: module example.com/nilchannel
After that, swap in each program as the new main.go. Transcripts come from go run -trimpath main.go. The -trimpath flag shortens file paths in the trace, you see ./main.go:9 instead of a long absolute path. Line numbers match either way. Tested with Go 1.26.4.
The working baseline: make, send, close, range
Before the failures, the pattern every fix returns to. The channel is created with make, the producer sends its values and then closes the channel, and the consumer ranges until the close ends the loop:
// main.go
package main
import "fmt"
func main() {
ch := make(chan int)
go func() {
ch <- 1
ch <- 2
close(ch)
}()
for v := range ch {
fmt.Println("received", v)
}
fmt.Println("range done")
}
$ go run -trimpath main.go
received 1
received 2
range done
The documentation for close gives the contract this shape follows: closing should be executed only by the sender, never the receiver, and it shuts the channel down after the last sent value is received. Here the one goroutine that sends is the one that closes, exactly once, after its last send. The range loop receives both values and then ends on its own, because ranging over a channel stops when the channel is closed and drained. Hold this shape in mind: every broken program below is this one with the make removed, and every fix is a way of putting the make back in the right place.
The panic itself: close of a channel that was never made
Here is the title panic in its smallest form. One declaration, no make, one close:
// main.go
package main
import "fmt"
func main() {
var ch chan int
fmt.Println("before close")
close(ch)
fmt.Println("after")
}
$ go run -trimpath main.go
before close
panic: close of nil channel
goroutine 1 [running]:
main.main()
./main.go:9 +0x51
exit status 2
The trace is short because there is almost nothing in it: no library frame, no helper, just main.main at ./main.go:9, the close(ch) line itself. The after line never prints. When you meet this panic in a real program, the trace points at the close, but the bug is wherever the channel was supposed to be made and was not. The declaration compiled, the variable passed around, and the program ran until the first operation that refuses a nil channel outright — which is close, since sends and receives on the same value would have blocked quietly instead of announcing the problem.
A send on a nil channel blocks forever
A send on the same nil value does not panic. It blocks, permanently, because no receiver can ever arrive on a channel that does not exist. Blocking forever cannot be photographed directly — a hung program prints nothing more — so this program races the send against a timer in a select. The timer winning is the proof:
// main.go
package main
import (
"fmt"
"time"
)
func main() {
var ch chan int
select {
case ch <- 1:
fmt.Println("sent")
case <-time.After(200 * time.Millisecond):
fmt.Println("send blocked: timeout won")
}
fmt.Println("main done")
}
$ go run -trimpath main.go
send blocked: timeout won
main done
The send case is never chosen. That is not a scheduling accident: the specification says a send on a nil channel blocks forever, so the only case that can ever proceed is the timeout. In production code, without the timer, this shape is a goroutine leaked forever and a pipeline stage that silently stops — no panic, no log line, just work that never arrives. When a program hangs with a goroutine parked on a send, check whether the channel it sends on was ever made before you check anything about buffering or receivers.
A receive from a nil channel blocks forever
The mirror image behaves the same way. A receive from a nil channel blocks forever, again by the specification's direct statement, and the same timer race proves it:
// main.go
package main
import (
"fmt"
"time"
)
func main() {
var ch chan int
select {
case v := <-ch:
fmt.Println("received", v)
case <-time.After(200 * time.Millisecond):
fmt.Println("receive blocked: timeout won")
}
fmt.Println("main done")
}
$ go run -trimpath main.go
receive blocked: timeout won
main done
Same verdict, other direction: the receive case is never chosen, and only the timeout lets main finish. Keep this program in mind for the contrast two sections down, because a closed channel behaves in exactly the opposite way on receive — it returns immediately — and confusing nil with closed is how this family of bugs gets misdiagnosed. Nil means no channel and no value, ever. Closed means a real channel that has promised no more values.
A nil channel disables a select case
Put a nil channel and a ready channel in the same select, and the ready case wins every time — not by luck, but because the nil case can never proceed at all:
// main.go
package main
import "fmt"
func main() {
var off chan int
ready := make(chan string, 1)
ready <- "ready value"
select {
case v := <-off:
fmt.Println("nil case", v)
case v := <-ready:
fmt.Println("ready case:", v)
}
fmt.Println("main done")
}
$ go run -trimpath main.go
ready case: ready value
main done
The specification carries the general form of this rule: since communication on nil channels can never proceed, a select with only nil channels and no default case blocks forever. With one real channel present, the select simply proceeds as if the nil case were not written. That is a hazard when the nil is an accident — a case you believe is live has been silently off since the program started — and it is a tool when the nil is deliberate, which the second-to-last program puts to work.
The realistic shape: a constructor that returns the unmade channel
Real code rarely writes var ch and close(ch) on adjacent lines. The missing make usually hides behind a constructor, a struct field, or a setup function: the value is declared in one place, returned or stored, and closed somewhere else by code that reasonably assumed it was made. Here is that shape at its smallest:
// main.go
package main
import "fmt"
func newStream() chan int {
var ch chan int
return ch
}
func main() {
ch := newStream()
fmt.Println("before close")
close(ch)
fmt.Println("after")
}
$ go run -trimpath main.go
before close
panic: close of nil channel
goroutine 1 [running]:
main.main()
./main.go:14 +0x51
exit status 2
Same message, same short trace, and the frame still names the close at ./main.go:14 — not the function that failed to make the channel. newStream compiled and returned a perfectly valid value of type chan int; it just returned the zero value. This is why the hunt for this panic starts at construction, not at the trace: search for every place the channel value originates — every constructor, every struct literal, every field assignment — and confirm that one of them runs make on every path, including error paths and the branch where setup was skipped because a feature was off. A channel field that is only made when a feature is enabled, paired with a close that runs unconditionally at shutdown, is this program wearing a larger costume.
Closed is not nil: receive from a closed channel returns at once
The contrast that separates the states. This channel is made, filled, and closed, and both receives return immediately — the first with the buffered value, the second with the zero value and ok set to false:
// main.go
package main
import "fmt"
func main() {
ch := make(chan int, 1)
ch <- 7
close(ch)
v, ok := <-ch
fmt.Println("first:", v, ok)
v, ok = <-ch
fmt.Println("second:", v, ok)
fmt.Println("main done")
}
$ go run -trimpath main.go
first: 7 true
second: 0 false
main done
The close documentation states this behavior: after the last value has been received from a closed channel, any receive from it will succeed without blocking, returning the zero value for the channel element, and the two-value form sets ok to false for a closed and empty channel. The specification says the same for the receive operator: a receive on a closed channel can always proceed immediately, yielding the zero value after any previously sent values have been received. Set that beside the receive program above it. Closed: returns at once, zero value, ok false, repeatable forever. Nil: never returns at all. If a receive is hanging, the channel is not closed-and-drained; it is nil, or it is open with no sender coming — and those two remaining causes are separated by checking the construction, as the previous section describes.
The intentional nil: turning a finished case off
Now the deliberate side, the idiom the accidental shapes imitate. This loop works through one channel, and when that channel closes it assigns nil to the variable — which disables that select case — and only then enables the second channel, which runs to its own close and is disabled the same way:
// main.go
package main
import "fmt"
func main() {
a := make(chan string, 2)
a <- "a1"
a <- "a2"
close(a)
var b chan string
for a != nil || b != nil {
select {
case v, ok := <-a:
if !ok {
fmt.Println("a closed, disabling its case")
a = nil
b = make(chan string, 1)
b <- "b1"
close(b)
continue
}
fmt.Println("from a:", v)
case v, ok := <-b:
if !ok {
fmt.Println("b closed, disabling its case")
b = nil
continue
}
fmt.Println("from b:", v)
}
}
fmt.Println("both done")
}
$ go run -trimpath main.go
from a: a1
from a: a2
a closed, disabling its case
from b: b1
b closed, disabling its case
both done
Every line is determined. While b is nil its case cannot be chosen, so the first phase drains a in order. When a closes, the receive reports ok false, the code sets a to nil, and from that iteration on the dead case is out of the select entirely — which matters, because receiving from a closed channel is always ready, so leaving a closed channel in a select would spin the loop on zero values forever. The counter here is the pair of variables themselves: the loop ends when both are nil, meaning both channels have closed and been retired. This is the standard shape for merging or sequencing channels of unknown length: a channel participates while it is live, and nil is its retirement. The difference between this program and the accidental ones is nothing but intent and placement — the nil here is assigned after a close, by the consumer, on purpose, instead of arriving from a constructor that forgot make.
The fix: make the channel at construction, close it in the owner
Return to the broken constructor. The fix is one line in the place the value is born — the declared variable becomes a made channel — and the rest of the program, including the caller's close, stays exactly as it was:
// main.go
package main
import "fmt"
func newStream() chan int {
ch := make(chan int, 2)
ch <- 1
ch <- 2
return ch
}
func main() {
ch := newStream()
close(ch)
for v := range ch {
fmt.Println("received", v)
}
fmt.Println("stream done")
}
$ go run -trimpath main.go
received 1
received 2
stream done
Same external shape as the panicking program: a constructor hands a channel to main, and main closes it. The difference is entirely inside the constructor, where make(chan int, 2) replaces the bare declaration. The buffered values survive the close — closing a channel does not discard what is already in it — so the range loop drains both values and ends. Two placement rules generalize from this pair of programs. First, a channel is made where its owner is defined: inside the constructor, at the struct literal, in the setup that creates the thing the channel serves — never left for the caller to guess at. Second, the close belongs to the sending side, exactly once: the close documentation's rule is that close should be executed only by the sender, never the receiver, to signal that no more values will be sent. A constructor that returns a channel should say, in its shape if not its comments, who that sender is.
The fixes, ranked
Make the channel at construction. Every channel value originates somewhere — a constructor, a literal, a setup function. That place runs make, on every path. A returned or stored channel that was only declared is this panic waiting for its first close, or a silent hang waiting for its first send or receive.
Treat the trace as a pointer to the close, not to the bug. The panic frame names the close call because close is where nil is refused. Audit backwards from there to every origin of the value, including feature-gated setup branches and error paths that skip the make.
Separate nil from closed when you diagnose a hang. A receive that never returns is a nil channel or an open channel with no sender; it is never a closed channel, because a closed channel returns the zero value and ok false at once. A send that never proceeds on a channel nobody will receive from is the same check in the other direction.
Close from the sender, exactly once, and only on a made channel. Close is the sender's signal that no more values will be sent. Receivers do not close, cleanup code does not close channels it did not make or own, and a defensive close on a maybe-nil channel is not defensive — it is this panic.
Use nil deliberately, and only as a retirement. Assigning nil to a finished channel disables its select case and is the correct way to retire it from a loop. Keep the deliberate nil assignments visibly separate from construction: nil is assigned after a close, by the code running the select, never returned from a constructor as a stand-in for a real channel.
Which fix for which cause
- Trace names a
close, channel came from a constructor or field Cause: The origin declared the variable and never ranmake, or ran it only on a setup branch that was skipped. Fix: Runmakeat the origin, on every path, and audit the branches that can reach theclosewithout passing it - Goroutine parked forever on a send, no panic Cause: The channel is nil, so the send can never proceed, whatever receivers the code believes exist. Fix: Find the value's origin and make the channel there; confirm with the select-and-timer shape if the state is unclear
- Goroutine parked forever on a receive, no panic Cause: The channel is nil, or open with no sender left. It is not closed — a closed channel would have returned the zero value with
okfalse. Fix: Check construction first for the missingmake; then account for the senders - A select case never fires, everything else works Cause: The case's channel is nil, so the case is never ready — an unmade channel, or a finished channel that was retired and never replaced. Fix: If the case should be live, make its channel at construction; if it was retired on purpose, the nil is correct and the expectation is wrong
- A select loop spins on zero values Cause: A closed channel was left in the select, where its receive is always ready. Fix: On
okfalse, assign nil to the channel variable to disable the case, and end the loop when every channel has been retired
Checklist
- Search for every declaration of the channel type —
vardeclarations, struct fields, constructor bodies — and confirm each value that escapes is made withmakeon every path, including error and feature-off paths. - For every
close, name the sender that owns the channel and themakethat created it. Acloseyou cannot pair with both does not ship. - Check shutdown and cleanup code for closes of channels it does not own, and for closes that run even when the setup that makes the channel was skipped.
- When a goroutine hangs on a channel operation, determine which of the three states the channel is in — nil, open, or closed — before changing buffering, adding receivers, or adding senders; each state has a different fix and the wrong one changes nothing.
- List every place nil is assigned to a channel on purpose. Each one should follow a close, inside the code that runs the select, as a retirement — never as a placeholder for a channel a constructor meant to make.
- Confirm no receiver closes a channel, and no channel is closed twice along any path, including retries and deferred cleanup.
Related guides on this site
- Fix Go "panic: close of closed channel", the neighboring close panic: there the channel was made and closed correctly once, and the second close is the bug — here the channel was never made, and the first close is already too many.
- Fix Go "panic: send on closed channel", the third member of the family: a send after the sender's signal, where this guide's sends on a nil channel never panic at all — they block forever instead.
Sources
- The Go Programming Language Specification, for the channel rules (a nil channel is never ready for communication), for send statements (a send on a nil channel blocks forever; a send on a closed channel proceeds by causing a run-time panic), for receive operations (receiving from a nil channel blocks forever; a receive operation on a closed channel can always proceed immediately, yielding the element type's zero value after any previously sent values have been received), and for select statements (since communication on nil channels can never proceed, a select with only nil channels and no default case blocks forever).
- The Go Programming Language, Package builtin, for
close(the close built-in function closes a channel, which must be either bidirectional or send-only; it should be executed only by the sender, never the receiver, and has the effect of shutting down the channel after the last sent value is received; after the last value has been received from a closed channel, any receive from it will succeed without blocking, returning the zero value for the channel element; the formx, ok := <-cwill also set ok to false for a closed and empty channel).
Keep Exploring
The full Go hub \u2014 slices and maps, concurrency, and the standard library.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.