# panic: 关闭 nil channel

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

## 根因

对未用 make() 初始化、或经条件赋值后变为 nil 的 channel 变量调用了 close()。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   type Worker struct {
    jobs chan Job
}
func NewWorker() *Worker {
    return &Worker{jobs: make(chan Job, 16)}
}
   ```
2. **** (90% 成功率)
   ```
   var once sync.Once
once.Do(func() { close(w.jobs) })
   ```

## 无效尝试

- **** — This masks the real bug: the channel was supposed to be created. Downstream senders/receivers now block forever on a nil channel instead of panicking, turning a crash into a hang. (70% 失败率)
- **** — Hides the defect and leaks the goroutines that are waiting on the never-created channel. (85% 失败率)
