go
runtime_error
ai_generated
true
panic:关闭已关闭的 channel
panic: close of closed channel
ID: go/panic-close-of-closed-channel
80%修复率
90%置信度
0证据数
2024-04-02首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0+ | active | — | — | — |
根因分析
close(ch) 被调用多次,通常来自多条清理路径或多个 defer 关闭。
English
close(ch) invoked more than once, often from multiple cleanup paths or deferred closes.
解决方案
-
95% 成功率
var once sync.Once once.Do(func() { close(ch) }) -
93% 成功率
go func() { defer close(ch) for v := range src { ch <- v } }()
无效尝试
常见但无效的做法:
-
70% 失败
Silently swallows a logic bug; downstream consumers may still observe inconsistent state.
-
95% 失败
Multiple goroutines closing the same channel guarantees the panic.