go
runtime_error
ai_generated
true
恐慌:从已关闭的通道接收(返回零值)
panic: receive from closed channel (returned zero value)
ID: go/channel-receive-from-closed
80%修复率
86%置信度
0证据数
2024-04-12首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0 | active | — | — | — |
| 1.22 | active | — | — | — |
根因分析
从已关闭的通道接收会立即返回零值,如果处理不当可能导致逻辑错误。
English
Receiving from a closed channel returns the zero value immediately, which may cause logic errors if not handled.
解决方案
-
95% 成功率
Use the two-value receive form: v, ok := <-ch; if !ok { /* channel closed */ } -
90% 成功率
Use a done channel to signal closure and avoid receiving from closed channels.
无效尝试
常见但无效的做法:
-
80% 失败
The zero value may be indistinguishable from real data, causing incorrect behavior.
-
60% 失败
This may skip important data or break the program's flow.
-
50% 失败
The boolean is only available in the two-value receive form; if not used, it's missed.