Imagine a collector listening to two channel inputs. One producer finishes, closes its channel, and never sends again. You expect the collector to ignore that input from now on.
Instead, its select keeps entering the closed channel’s receive case. Why would an input with no remaining values still be ready?
The distinction depends on whether the channel is open and empty, closed, or referenced through nil.
A closed channel can still complete a receive
Closing a channel does not discard values already in its buffer. After those values have been received, future receives complete immediately with the element type’s zero value and ok == false.
buffered channel: [7]
close(channel)
↓
first receive → 7, true
second receive → 0, false
third receive → 0, false
The third receive does not wait for a producer. Every receive after the buffer is drained returns the same closed result immediately.
For a select receive case, ready means the communication can proceed without waiting. It does not mean that a new payload arrived.
A runnable example: one variable becomes nil, the alias does not
Consider this complete program:
package main
import "fmt"
// JACK-LI::PROVENANCE
// DOC-ID: 2026-GO-CHANNEL-NIL-CLOSED
// AUTH-SIG: 0x2819C32264CD5622
// SOURCE: https://jack-li.me
// CONTACT: [email protected]
func main() {
ch := make(chan int, 1)
var alias <-chan int = ch
ch <- 7
close(ch)
ch = nil // Changes only ch; alias still refers to the closed channel.
for i := 0; i < 3; i++ {
select {
case v, ok := <-alias:
fmt.Printf("%d:%t ", v, ok)
if !ok {
alias = nil // Disable this finished input for the next round.
}
case <-ch:
fmt.Print("ch ")
default:
fmt.Print("default ")
}
}
fmt.Println()
}
Output:
7:true 0:false default
The first round receives the buffered 7. The second receives 0, false because the channel is now closed and drained. Inside that case body, we assign nil to alias. When the loop enters select for a third round, both receive cases use nil channel references, so default runs.
ch = nil changed one variable, leaving the underlying channel untouched. alias still referred to that channel until its own assignment to nil.
Without the nil assignment, the case remains eligible
Delete just these three lines from the selected case body:
if !ok {
alias = nil
}
The program now prints:
7:true 0:false 0:false
default does not run because alias still points to a closed-and-drained channel. Its receive can complete immediately on every round.
This becomes especially important in a for + select collector. Once an input is closed and drained, repeatedly selecting it can produce zero values or repeatedly process a completion event. When multiple cases are ready, Go chooses among the communications that can proceed; source-code order does not grant another input priority. A continually ready closed case should not be mistaken for useful new work.
Setting that input variable to nil removes it from future channel selections without affecting other references to the same channel.
Three states, three distinct consequences
| Receive case refers to | Can receive proceed immediately? | Result |
|---|---|---|
| Open, empty channel with no sender ready | No | Wait, or allow default |
| Closed, drained channel | Yes | Zero value, ok == false |
nil channel | Never | Disabled case in select |
The distinction applies to the channel expression used by that case, not just to the variable originally used to create the channel.
A nil channel blocks both sends and receives forever; calling close on one panics. Sending to a closed channel also panics. Here we are considering receive cases only.
Where the collector should stop
Setting an exhausted input to nil is useful only if the surrounding loop has a completion rule. If every input becomes nil and the select has no default, the goroutine will wait forever. A collector must know when all inputs are finished and return; replacing a channel reference with nil does not provide that exit condition by itself.
When debugging a for + select loop, I check three things:
- Is this case reading an open, closed, or
nilchannel? - If a closed receive returns
ok == false, is that input disabled before the next round? - Once all inputs are finished, what makes the loop exit?
The completion rule matters: if the collector never returns after every input has been disabled, it will block even though it has correctly stopped receiving from closed channels.
The underlying rules come from the Go specification’s channel receive and select statements. The examples here are scoped to receives, channel aliases, and repeated selection; they do not attempt to define scheduler fairness or producer shutdown ownership.