go
runtime_error
ai_generated
true
恐慌:从已关闭的通道接收(在select中)
panic: receive from closed channel (in select)
ID: go/goroutine-select-on-closed-channel
80%修复率
85%置信度
0证据数
2025-06-30首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0 | active | — | — | — |
| 1.22 | active | — | — | — |
根因分析
在已关闭的通道上select会立即接收零值,如果不处理可能导致意外行为。
English
Selecting on a closed channel immediately receives zero values, which may cause unexpected behavior if not handled.
解决方案
-
95% 成功率 Use two-value receive to check if channel is closed
select { case val, ok := <-ch: if !ok { // channel closed } // process val } -
85% 成功率 Set channel to nil after close to disable it
close(ch) ch = nil select { case <-ch: // blocked because nil }
无效尝试
常见但无效的做法:
-
Ignoring closed channel in select
70% 失败
Closed channel always returns zero value immediately; select may not behave as expected.
-
Using recover() to catch zero value
90% 失败
Receive from closed channel doesn't panic; it returns zero value with ok=false.