Fix Go "panic: send on closed channel" — Close of Closed and Nil Channels, Receiving From a Closed Channel, and select
This panic means close and send met on the same channel in the wrong order: the channel was closed to say no more values will come, and then a send tried to put one more value on it. Each section below takes one shape — the smallest send after close, a second close, a close of a nil channel, a loop that closes too early, the two receive shapes that close makes safe, a select that cannot save a closed send, and the recover contrast — shows the exact output it prints, and gives the fix that keeps the close in one owner's hands or stops the sender before the send.
Written November 20, 2026 · Tested with Go 1.26.4 · 21 min read
- The first failure: one send after one close
- Reading the message: three panic texts, one family
- Closing twice: the second close panics
- Closing a nil channel: the channel was never made
What this guide is
panic: send on closed channel is a run-time panic: the program built, started, and then sent a value on a channel that had already been closed. The specification states the rule in two places that say the same thing. Under send statements: a send on a closed channel proceeds by causing a run-time panic. Under the built-in function close: sending to or closing a closed channel causes a run-time panic. The built-in close(ch) records that no more values will be sent on the channel, so a send after that record is a contradiction the runtime refuses to carry out. Every send shape in this guide is that one contradiction met from a different direction: a close placed one statement too early, a sender in another part of the program that never learned the channel was finished, and a loop whose close fires on an early pass while its sends continue.
The close side has two panic shapes of its own, and both are in the same specification passage. Closing a closed channel causes a run-time panic, so a second close on the same channel prints panic: close of closed channel. Closing the nil channel also causes a run-time panic, so close on a channel variable that was declared and never made prints panic: close of nil channel. A channel that was never made is nil because the zero value for channels is nil, the same zero-value rule that covers pointers, interfaces, slices, and maps.
Receiving is the opposite case, and the asymmetry is the point of the design. 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. Close is a signal aimed at receivers: it tells them the stream is complete, it lets a range over the channel end, and it never endangers a receive. The two-value receive form reports the boundary directly, because the value of ok is false when the received value is a zero value generated because the channel is closed and empty. The danger sits only on the send and close side, which is why the fixes in this guide are about who may send, who may close, and how a sender learns to stop.
The examples here use Go 1.26.4. Every example lives in a single file named main.go inside one module, created once from an empty directory:
$ mkdir closedchan
$ cd closedchan
$ go mod init example.com/closedchan
go: creating new go.mod: module example.com/closedchan
Each program that follows replaces the contents of that main.go. The panic transcripts are shown as printed by go run -trimpath main.go: the -trimpath flag only changes how file positions print in the trace — ./main.go:8 here, where a plain run prints the same line number behind the full directory path on your machine. Tested with Go 1.26.4.
The first failure: one send after one close
The smallest program that earns the panic makes a channel, closes it, and sends on it:
// main.go
package main
import "fmt"
func main() {
ch := make(chan int, 1)
close(ch)
ch <- 1
fmt.Println("sent")
}
$ go run -trimpath main.go
panic: send on closed channel
goroutine 1 [running]:
main.main()
./main.go:8 +0x3a
exit status 2
The buffer size of 1 changes nothing: room in the buffer is not the condition the send checks. The send checks whether the channel is closed, finds that it is, and panics at line 8, the send statement itself. The sent line never prints. This shape looks artificial in five lines, and it is the shape real programs arrive at honestly: the close and the send are rarely this close together in working code, but the rule does not measure distance. A close in a cleanup path, a shutdown path, or an error path, and a send in a worker that is still running, meet the same way these two statements meet here.
The repair at this size is ordering: a channel is closed after its last send, never before a send that still has to happen. In a single goroutine that is a matter of reading the function top to bottom. Across goroutines it becomes an ownership question, and ownership is the fix the rest of this guide builds toward: the goroutine that sends is the goroutine that closes, it closes when its sends are done, and no other goroutine closes a channel it sends on.
Reading the message: three panic texts, one family
This family prints three different panic texts, and the text itself names which rule fired. panic: send on closed channel means a send statement executed on a closed channel. panic: close of closed channel means close executed on a channel that was already closed. panic: close of nil channel means close executed on a channel whose value is nil. The three texts share a structure with the other run-time panics in this series: the first line states the rule in plain words, a blank line follows, and the trace names the function and the line where the offending statement stands. Unlike the nil pointer panic, there is no signal line: no memory access failed, so there is no SIGSEGV report, only the panic and its trace.
The trace line is the statement that broke the rule, and it is worth reading it that way rather than as the place where the bug lives. For a send, the trace names the send, and the bug is wherever the close came from and why the sender did not know. For a second close, the trace names the second close, and the bug is that two places in the program each believed closing was their job. For a nil close, the trace names the close, and the bug is upstream, where the channel was supposed to be made and was not. In each case the fix sits at the other end of the relationship from the line in the trace.
Closing twice: the second close panics
A channel can be closed once. The second call to close on the same channel is not a harmless repeat:
// main.go
package main
import "fmt"
func main() {
ch := make(chan int)
close(ch)
close(ch)
fmt.Println("closed")
}
$ go run -trimpath main.go
panic: close of closed channel
goroutine 1 [running]:
main.main()
./main.go:8 +0x30
exit status 2
The specification's wording covers this in the same sentence as the send case: sending to or closing a closed channel causes a run-time panic. There is no query that asks a channel whether it is already closed, and there is no form of close that tolerates a repeat. Code that closes from two places — a normal completion path and an error path, a producer and a timeout handler — carries this panic on the run where both paths fire. The repair is the same ownership rule as for the send: exactly one place in the program closes a given channel, and every other path that wants the channel finished signals that owner instead, with a done channel or a return value, and lets the owner perform the single close.
Closing a nil channel: the channel was never made
A declared channel variable holds nil until make assigns a real channel to it, and close on that nil value panics with its own text:
// main.go
package main
import "fmt"
func main() {
var ch chan int
close(ch)
fmt.Println("closed")
}
$ go run -trimpath main.go
panic: close of nil channel
goroutine 1 [running]:
main.main()
./main.go:7 +0x15
exit status 2
The specification states this case separately: closing the nil channel also causes a run-time panic. The nil channel has a consistent set of behaviors, and they split by operation. A send on a nil channel blocks forever, and receiving from a nil channel blocks forever, because a nil channel is never ready for communication. Close is the exception: instead of blocking, it panics at once. This shape appears when a channel field inside a struct is left at its zero value, when a channel is meant to be created inside a conditional branch that a given run does not take, or when a setup function returns a channel that one of its paths never made. The repair is construction: make the channel at the point where the value that owns it is created, so the nil state is not available to the code that later closes, or test the channel against nil at the close site when nil is a legitimate state that means there is nothing to close.
A loop that closes on an early pass: the producer pattern
The realistic send shape is a producer that sends in a loop and closes the channel from inside that loop one pass too early:
// main.go
package main
import "fmt"
func main() {
ch := make(chan int, 3)
for i := 1; i <= 3; i++ {
ch <- i
fmt.Println("sent", i)
if i == 1 {
close(ch)
}
}
}
$ go run -trimpath main.go
sent 1
panic: send on closed channel
goroutine 1 [running]:
main.main()
./main.go:8 +0x5e
exit status 2
The first pass sends 1 into the buffer and prints sent 1. The close at the bottom of that pass succeeds, because closing is legal at any point. The second pass reaches the send at line 8 and panics. The buffer still has room for two more values, and the loop still has two more values to send, and neither fact matters: the close has already recorded that no more values will be sent. In working code this shape arrives as a close placed in a branch the loop can take early — a close after the first batch, a close in an error arm that falls through to the next iteration, a close in a deferred call inside a helper the loop calls per item while the sends continue in the caller. The repair is placement: the close belongs after the loop, on the path where the sends are finished, in the same function that owns the sends. A close that a loop body can reach while the loop still has sends ahead of it is this panic waiting for the pass that takes the branch.
Receiving from a closed channel: the safe direction, with the ok check
Receiving from a closed channel is defined behavior, and the two-value form shows exactly where the sent values end:
// main.go
package main
import "fmt"
func main() {
ch := make(chan int, 2)
ch <- 10
ch <- 20
close(ch)
for i := 0; i < 3; i++ {
v, ok := <-ch
fmt.Println(v, ok)
}
}
$ go run -trimpath main.go
10 true
20 true
0 false
The first two receives return the values sent before the close, each with ok as true, in the order they were sent. The third receive proceeds at once, without blocking, and returns the zero value 0 with ok as false: that pair is the closed-and-empty signal. After calling close, and after any previously sent values have been received, receive operations return the zero value for the channel's type without blocking, and the multi-valued form is how a receiver tells a real zero value that a sender sent from the zero values that close generates. This is the mechanism a range over a channel relies on: the range receives until the channel is closed and empty, and then the loop ends. A receiver never needs to fear a closed channel, and a receiver is never the party that should close one: closing is how a sender tells receivers the stream is complete, and a receiver that closes breaks every other receiver and every sender still working.
Receiving without the ok check: zero values with no end
The same receive without the second value loses the boundary, and a bounded loop shows what an unbounded one would do:
// main.go
package main
import "fmt"
func main() {
ch := make(chan int, 1)
ch <- 7
close(ch)
for i := 0; i < 4; i++ {
fmt.Println(<-ch)
}
}
$ go run -trimpath main.go
7
0
0
0
The first receive prints the sent 7. The next three receives each return at once with the zero value, and they would keep returning zero for as many receives as the program asks for: a closed and drained channel never blocks a receiver. A loop that receives in this form and waits for a condition on the value to stop it never sees its stop condition, because the zero value is not the sentinel the sender ever sent. It is not a panic and it is not a deadlock; it is a quiet spin or a stream of phantom zero values flowing into whatever the loop feeds. The repair is to receive in the form that can see the end: the two-value form with its ok check, or a range over the channel, which ends itself when the close has been drained. Where a zero value is a legitimate value in the stream, the ok form is the only receive that can tell the stream's own zeros from the zeros close produces.
A select with a send on a closed channel: the default case does not save it
Wrapping a send in a select does not soften the rule, and a default case does not rescue it:
// main.go
package main
import "fmt"
func main() {
ch := make(chan int, 1)
close(ch)
select {
case ch <- 1:
fmt.Println("sent")
default:
fmt.Println("default")
}
fmt.Println("after")
}
$ go run -trimpath main.go
panic: send on closed channel
goroutine 1 [running]:
main.main()
./main.go:9 +0x47
exit status 2
A select waits until one of its cases can proceed, and chooses the default case when none can. A send on a closed channel is not a case that cannot proceed: under the send rule it proceeds by causing a run-time panic, so the select takes that case and the panic fires at line 9, the send case, without the default ever being chosen and without after printing. This matters because the select with default is the standard non-blocking send, and it is easy to read it as a safe send that falls back when the send is impossible. It falls back when the channel is open and no receiver is ready, or the buffer is full. A closed channel is a different state from a busy one, and the fallback does not cover it. A sender that cannot rule out a closed channel has to rule it out by design — ownership and a done channel, as in the fixed program below — not by wrapping the send in a construct that was built to choose among open channels.
The contrast: a panic can be recovered
This panic is an ordinary panic once the runtime raises it, and a deferred recover catches it:
// main.go
package main
import "fmt"
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Println("recovered:", r)
}
}()
ch := make(chan int, 1)
close(ch)
ch <- 1
fmt.Println("after")
}
$ go run -trimpath main.go
recovered: send on closed channel
The deferred function prints the recovered value, the panic text on its own, and the program exits cleanly; after does not print, because the panic still unwound the function that raised it. The contrast that gives this its place is with the deadlock failure covered in the related guides: a fatal error, where all goroutines are asleep, is not a panic and recover cannot catch it, while this panic can be recovered and should instead be prevented. The legitimate uses are the same as for any panic: a probe that confirms which send in a suspect path fires first, and a boundary guard around work the program does not control. Around the program's own channel use, recover is the wrong tool: it learns that a send met a close by unwinding the goroutine that sent, discards whatever that goroutine was in the middle of, and still leaves the ownership defect that produced the meeting in place for the next run.
The fixed program
One program that applies each fix at once: the producing function is the only closer of its channel and closes after its sends, the consumer ranges until the close ends the loop, and a second sender is stopped by a done channel instead of by closing the channel it sends on:
// main.go
package main
import "fmt"
func produce(ch chan<- int) {
for _, v := range []int{1, 2, 3} {
ch <- v
}
close(ch)
}
func main() {
ch := make(chan int)
go produce(ch)
for v := range ch {
fmt.Println(v)
}
done := make(chan struct{})
out := make(chan int)
close(done)
select {
case out <- 99:
fmt.Println("sent 99")
case <-done:
fmt.Println("stopped before send")
}
fmt.Println("done")
}
$ go run main.go
1
2
3
stopped before send
done
produce sends 1, 2, and 3 and then closes ch, in that order, from the one function that sends on it. The range in main prints the three values and ends when the close has been drained, with no count kept by hand and no ok test needed, because the range is the receive form built for a closed end. The second half shows the stop signal running the other way: main closes done, a channel nobody sends values on, and the select takes the done case and prints stopped before send instead of attempting the send on out. Closing a done channel is the safe close, because a done channel carries no values: closing it broadcasts the stop to every receiver at once, no sender can ever panic on it, and the data channel is left for its own owner to close. Nothing in the program recovers, and nothing needs to: each channel has one closer, each close comes after that channel's last send or carries no sends at all, and each receiver has a form that can see the end.
The fixes, ranked
Give each channel one closer, and make it the sender. The goroutine or function that sends on a channel is the one that closes it, once, after its last send. This is ranked first because it removes both panic shapes at the source: with one closer there is no second close, and with the close placed after the sends there is no send after close from that owner, which is the only sender the design allows.
Stop extra senders with a done channel, never by closing their channel. When a receiver or a coordinator needs other goroutines to stop sending, it closes a separate done channel that those goroutines select on alongside their send. Closing the data channel under a sender that is still working manufactures the panic; a done channel carries the stop signal without touching the channel the senders own.
Close after the loop, not inside it. A producer that sends in a loop closes after the loop, on the completion path. A close reachable from a loop body, a per-item helper, or an early branch is placed where a later pass can send into it, and moving it past the loop is the whole fix for that shape.
Receive in a form that can see the end. Use the two-value receive and act on ok as false, or range over the channel. A single-value receive from a channel that will be closed cannot tell a sent zero from a generated one and cannot tell the stream has ended.
Make the channel where its owner is made. A channel field or variable that later code will close is created with make at construction, so the close never meets a nil channel. Where nil is a legitimate state, test for it at the close site and treat it as nothing to close.
Do not answer the panic with recover in code that owns the channel. The recovered program above is a probe and a boundary pattern. Around your own sends and closes, recover converts an ownership defect into an unwinding, and the defect survives to fire again on the next run.
Which fix for which cause
| What the message and code show | Cause | Fix |
|---|---|---|
panic: send on closed channel at a send whose close is in another path |
A sender outlived the close, or the close ran on a cleanup or shutdown path | One closer: the sending owner closes after its last send; stop other senders with a done channel |
panic: send on closed channel one pass after a close inside a loop |
The close sits in the loop body or a per-item helper while sends continue | Move the close after the loop, on the completion path |
panic: send on closed channel at a send case inside a select |
A closed channel in a select send case proceeds by panicking; default does not cover it |
Rule out the closed state by ownership and done channels before the select |
panic: close of closed channel |
Two places each closed the same channel | Exactly one close site per channel; other paths signal the owner |
panic: close of nil channel |
The channel was declared, or left as a struct field, and never made | make the channel at construction, or test for nil at the close site |
| No panic, but a receive loop spins or prints endless zeros | A single-value receive cannot see that a closed channel is drained | Receive with the ok form, or range over the channel |
Checklist
- Read the panic text first: it names the rule that fired — a send on a closed channel, a close of a closed channel, or a close of a nil channel — and each text points at a different fix.
- Take the trace line as the statement that broke the rule, then find the other party: the close that preceded the send, the first close that preceded the second, or the missing
makethat preceded the nil close. - Audit close sites: every channel in the program should have exactly one place that closes it, and that place should belong to the sender.
- Audit senders that can outlive their channel: each one selects on a done channel, or is joined before the close, so no send can arrive after it.
- Audit loop bodies for closes, including closes inside helpers the loop calls and closes in deferred calls registered per pass.
- Audit receives: any receive loop over a channel that will be closed uses the
okform or a range, never a bare receive waiting on a value condition.
Related guides on this site
- Fix Go "fatal error: all goroutines are asleep - deadlock!" — Unbuffered Channels, Nil Channels, select, and Unclosed Ranges — the failure this guide's contrast is measured against: a fatal error, not a panic, which
recovercannot catch, arising from the nil and unbuffered channels this guide's rules also cover, where a send on a closed channel is an ordinary panic that ownership prevents. - Fix Go "panic: runtime error: invalid memory address or nil pointer dereference" — Nil Receivers, Returned Nil Pointers, and Typed Nil Interfaces — the sibling zero-value panic: a pointer used before it was given a value, where this guide's nil shape is a channel closed before it was ever made, and both traces name the use while the cause sits at the construction site.
- Fix Go "assignment to entry in nil map" Panic — Declared Maps, Struct Fields, and Nested Maps — the other declared-but-never-made panic: a map write that fails because the map was never made, where this guide's write-side failure is a send on a channel that was closed, and both are fixed by construction and by one clear owner for the value's life.
- Fix Go "panic: runtime error: index out of range" — Slice, Array, String, and Slice-Bounds Panics — the other everyday runtime panic, judged against a length; its message names the index and the length, where this guide's names the channel rule that fired, and both are ordinary panics that
recovercan catch and a check or an ownership rule prevents better.
Sources
- The Go Programming Language Specification — for send statements (a send on a closed channel proceeds by causing a run-time panic, and a send on a nil channel blocks forever), for the built-in function
close(sending to or closing a closed channel causes a run-time panic, closing the nil channel also causes a run-time panic, and aftercloseand after previously sent values have been received, receive operations return the zero value without blocking), for the receive operator (receiving from a nil channel blocks forever, a receive on a closed channel can always proceed immediately, yielding the zero value after previously sent values have been received, and the two-value form reports inokwhether the value was delivered by a send or generated because the channel is closed and empty), and for channel types (a nil channel is never ready for communication, and the multi-valued receive form reports whether a received value was sent before the channel was closed).
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.