# 恐慌：从已关闭的通道接收数据

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

## 根因

尝试从已关闭的通道接收数据，但仅当通道为nil时才会发生恐慌。

## 版本兼容性

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

## 解决方案

1. **Use comma-ok idiom to detect closed channel** (95% 成功率)
   ```
   v, ok := <-ch; if !ok { /* channel closed */ }
   ```
2. **Ensure channel is not nil before receive** (80% 成功率)
   ```
   if ch != nil { v := <-ch }
   ```

## 无效尝试

- **Checking channel with cap() before receive** — cap() does not indicate closed state. (90% 失败率)
- **Using recover() to ignore panic** — Recover does not prevent data corruption. (70% 失败率)
