go
runtime_error
ai_generated
true
panic: runtime error: receive from nil channel
ID: go/channel-receive-from-nil
80%Fix Rate
84%Confidence
0Evidence
2024-07-05First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.20 | active | — | — | — |
| 1.21 | active | — | — | — |
Root Cause
Attempting to receive from a channel that is nil, causing permanent block, but if in select with default, it may not panic; however, direct receive panics.
generic中文
尝试从 nil 通道接收,导致永久阻塞,但如果 select 中有 default 可能不会恐慌;然而直接接收会恐慌。
Workarounds
-
100% success Initialize channel before use
ch := make(chan int); go func() { ch <- 1 }(); val := <-ch -
90% success Use select with default to handle nil channel gracefully
select { case val := <-ch: use(val); default: // skip }
Dead Ends
Common approaches that don't work:
-
Checking ch == nil before receive and returning
30% fail
Check prevents panic but may cause silent data loss.
-
Using recover to catch panic
50% fail
Panic is recoverable but indicates logic error; data not received.