go runtime_error ai_generated true

恐慌:运行时错误:在nil通道上选择

panic: runtime error: select on nil channel

ID: go/goroutine-select-on-nil-channel

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2025-02-10首次发现

版本兼容性

版本状态引入弃用备注
1.18 active
1.19 active
1.20 active
1.21 active
1.22 active

根因分析

在select语句中使用nil通道,导致永久阻塞和潜在死锁。

English

Using a nil channel in a select statement, which causes a permanent block and potential deadlock.

generic

解决方案

  1. 95% 成功率 Initialize channels before use in select
    ch := make(chan int); select { case v := <-ch: // use v }
  2. 90% 成功率 Check for nil before select
    if ch != nil { select { case v := <-ch: // } }

无效尝试

常见但无效的做法:

  1. Adding default case without fixing nil 80% 失败

    Default case executes but nil channel still blocks if selected.

  2. Using recover() to ignore 90% 失败

    Does not fix nil channel issue.