go
runtime_error
ai_generated
true
panic: close of nil channel
ID: go/channel-close-nil-channel
80%Fix Rate
90%Confidence
0Evidence
2024-06-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.0+ | active | — | — | — |
Root Cause
Attempting to close a channel that has not been initialized (nil).
generic中文
试图关闭一个未初始化的 channel(nil)。
Workarounds
-
98% success
ch := make(chan int) // later close(ch)
-
90% success
var once sync.Once closeCh := func() { if ch != nil { once.Do(func() { close(ch) }) } }
Dead Ends
Common approaches that don't work:
-
80% fail
If the channel is nil, closing it is still a panic; you must avoid the close entirely
-
70% fail
Recovering hides the bug and may leave resources in inconsistent state