go
runtime_error
ai_generated
true
恐慌:运行时错误:在nil通道上选择
panic: runtime error: select on nil channel
ID: go/goroutine-select-on-nil-channel
80%修复率
86%置信度
0证据数
2025-02-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.19 | active | — | — | — |
| 1.20 | active | — | — | — |
| 1.21 | active | — | — | — |
| 1.22 | active | — | — | — |
根因分析
在select语句中使用nil通道,导致永久阻塞和潜在死锁。
English
Using a nil channel in a select statement, which causes a permanent block and potential deadlock.
解决方案
-
95% 成功率 Initialize channels before use in select
ch := make(chan int); select { case v := <-ch: // use v } -
90% 成功率 Check for nil before select
if ch != nil { select { case v := <-ch: // } }
无效尝试
常见但无效的做法:
-
Adding default case without fixing nil
80% 失败
Default case executes but nil channel still blocks if selected.
-
Using recover() to ignore
90% 失败
Does not fix nil channel issue.