# 恐慌：关闭已关闭的通道

- **ID:** `go/goroutine-channel-close-multiple`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

对已经关闭的通道再次调用close()，通常是由于多个协程试图关闭同一个通道。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.21 | active | — | — |
| 1.22 | active | — | — |

## 解决方案

1. **Use sync.Once to ensure close is called only once** (99% 成功率)
   ```
   var closeOnce sync.Once
closeOnce.Do(func() {
    close(ch)
})
   ```
2. **Designate a single goroutine to close the channel** (98% 成功率)
   ```
   // Only one goroutine has access to close(ch)
ch := make(chan int)
go func() {
    // close when done
    close(ch)
}()
   ```

## 无效尝试

- **Checking if channel is closed before closing** — No built-in way to check if a channel is closed without risking a race condition. (80% 失败率)
- **Using recover() to catch panic** — Recover does not prevent the panic from occurring; it only catches it after the fact. (85% 失败率)
