go runtime_error ai_generated true

panic: runtime error: select on nil channel

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2025-02-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active
1.19 active
1.20 active
1.21 active
1.22 active

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. Adding default case without fixing nil 80% fail

    Default case executes but nil channel still blocks if selected.

  2. Using recover() to ignore 90% fail

    Does not fix nil channel issue.