# panic: 关闭 nil channel

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

## 根因

试图关闭一个未初始化的 channel（nil）。

## 版本兼容性

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

## 解决方案

1. **** (98% 成功率)
   ```
   ch := make(chan int)
// later
close(ch)
   ```
2. **** (90% 成功率)
   ```
   var once sync.Once
closeCh := func() {
    if ch != nil {
        once.Do(func() { close(ch) })
    }
}
   ```

## 无效尝试

- **** — If the channel is nil, closing it is still a panic; you must avoid the close entirely (80% 失败率)
- **** — Recovering hides the bug and may leave resources in inconsistent state (70% 失败率)
