# panic：向已关闭的 channel 发送数据

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

## 根因

某个 goroutine 在另一个 goroutine 调用 close() 之后仍向该 channel 发送数据，违反了单一关闭者/关闭后不发送的规则。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.0+ | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   done := make(chan struct{})
go func() {
    for {
        select {
        case <-done:
            return
        case ch <- v:
        }
    }
}()
   ```
2. **** (88% 成功率)
   ```
   mu.Lock()
if !closed {
    ch <- v
}
mu.Unlock()
   ```

## 无效尝试

- **** — Recover only protects the current goroutine; other senders still panic and the channel remains closed. (85% 失败率)
- **** — len() is racy; the channel can be closed between the check and the send. (90% 失败率)
