go
data_error
ai_generated
true
error: unexpected zero value received (channel closed, ok=false ignored)
ID: go/receive-from-closed-channel-zero-value
80%Fix Rate
88%Confidence
0Evidence
2024-04-08First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.21 | active | — | — | — |
Root Cause
Reading from a closed channel returns the zero value immediately forever. Code that ignores the 'ok' bool treats zero values as real data, causing silent corruption.
generic中文
从已关闭的 channel 读取会永远立即返回零值。忽略 'ok' 布尔值的代码把零值当作真实数据,造成静默的数据损坏。
Workarounds
-
95% success
Use the comma-ok idiom: 'v, ok := <-ch; if !ok { return }' and break the loop on close -
93% success
Use 'for v := range ch { ... }' which terminates automatically when the channel closes
Dead Ends
Common approaches that don't work:
-
80% fail
Closed channel always returns immediately; sleep just delays the wrong value
-
85% fail
len is a snapshot and racy; does not distinguish closed from empty