# panic: 关闭 nil channel

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

## 根因

对一个从未用 make() 初始化的 channel 变量调用 close()，其值为 nil 零值。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   Initialize the channel at declaration: ch := make(chan int). Never use var ch chan int without assignment if it will be closed.
   ```
2. **** (93% 成功率)
   ```
   Use a struct constructor that always makes the channel, e.g. func NewWorker() *Worker { return &Worker{jobs: make(chan Job)} }.
   ```

## 无效尝试

- **** — Skipping the close leaves the channel open forever, so receivers block indefinitely; the underlying logic bug remains. (75% 失败率)
- **** — Creating a fresh channel at close time means no other goroutine holds a reference to it; the close is meaningless. (85% 失败率)
