go
runtime_error
ai_generated
true
fatal error: all goroutines are asleep - deadlock! (nil channel receive)
ID: go/channel-receive-from-nil-channel
80%Fix Rate
87%Confidence
0Evidence
2024-09-03First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.0+ | active | — | — | — |
Root Cause
A goroutine attempted to receive from a nil channel, which blocks forever. If all goroutines are blocked on nil channels, the runtime detects a deadlock.
generic中文
某个 goroutine 尝试从 nil channel 接收数据,这会永久阻塞。如果所有 goroutine 都阻塞在 nil channel 上,运行时会检测到死锁。
Workarounds
-
98% success
ch := make(chan int) // or ch := make(chan int, 1)
-
90% success
select { case v := <-ch: // use v case <-time.After(1 * time.Second): return errors.New("timeout: channel may be nil") }
Dead Ends
Common approaches that don't work:
-
90% fail
A nil channel is not the same as an uninitialized variable; you must explicitly create it with make().
-
85% fail
A select with a nil channel case will block if no other case is ready; default only helps if you want non-blocking behavior.