go
runtime_error
ai_generated
true
panic: 关闭已关闭的 channel
panic: close of closed channel
ID: go/channel-close-receive-panic
80%修复率
90%置信度
0证据数
2024-05-12首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0+ | active | — | — | — |
根因分析
试图关闭一个已经关闭的 channel。
English
Attempting to close a channel that has already been closed.
解决方案
-
98% 成功率
var once sync.Once closeCh := func() { once.Do(func() { close(ch) }) } -
95% 成功率
go func() { defer close(ch) // send values }()
无效尝试
常见但无效的做法:
-
80% 失败
Hides double-close bug, may lead to inconsistent state
-
70% 失败
No built-in way to check if channel is closed without receiving