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

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

## 根因

在通道关闭后向其发送数据会导致运行时恐慌。

## 版本兼容性

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

## 解决方案

1. **** (85% 成功率)
   ```
   ch := make(chan int)
close(ch)
// Use select with a done channel to avoid sending after close
select {
case ch <- 1:
default:
    fmt.Println("channel closed")
}
   ```
2. **** (90% 成功率)
   ```
   var mu sync.RWMutex
closed := false
// In sender:
mu.RLock()
if !closed { ch <- 1 }
mu.RUnlock()
// In closer:
mu.Lock()
close(ch)
closed = true
mu.Unlock()
   ```

## 无效尝试

- **** — Recovery doesn't prevent data loss or inconsistency; the sender state is corrupted. (60% 失败率)
- **** — len() doesn't indicate closed status; a closed channel can still have buffered items. (80% 失败率)
- **** — Mutex doesn't prevent closing while sending; race condition remains. (50% 失败率)
