# 恐慌：向已关闭的通道发送数据

- **ID:** `go/panic-runtime-error-send-on-closed-channel`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

尝试向已关闭的通道发送值，违反了通道的使用约定。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| go1.21 | active | — | — |
| go1.22 | active | — | — |
| go1.23 | active | — | — |

## 解决方案

1. ```
   Ensure the channel is closed only by the sender after all sends are complete. Use a sync.WaitGroup to coordinate goroutines, and close the channel in a dedicated goroutine after Wait.
   ```
2. ```
   Use a select statement with a default case to check if the channel is closed before sending, or use a separate closed channel for signaling.
   ```

## 无效尝试

- **Adding a time.Sleep before closing the channel to avoid race conditions.** — Race conditions are timing-dependent; adding sleep does not guarantee ordering and may hide the bug rather than fix it. (85% 失败率)
- **Using recover() in a deferred function to catch the panic.** — Recovering from a panic does not fix the root cause; the channel was already misused and data may be lost. (70% 失败率)
- **Closing the channel after all sends are done by adding a sync.WaitGroup but not calling WaitGroup.Wait() correctly.** — Incorrect WaitGroup usage leads to same race: close happens before all sends complete. (60% 失败率)
