go
runtime_error
ai_generated
true
panic: 运行时错误:无效内存地址或空指针解引用(发生在 goroutine 中)
panic: runtime error: invalid memory address or nil pointer dereference (in goroutine)
ID: go/goroutine-nil-pointer
80%修复率
86%置信度
0证据数
2024-07-02首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.20 | active | — | — | — |
| 1.22 | active | — | — | — |
根因分析
在派生的 goroutine 中解引用了空指针。由于 panic 发生在子 goroutine 中,会导致整个程序崩溃,父 goroutine 无法 recover。
English
A nil pointer is dereferenced inside a spawned goroutine. Because the panic happens in a child goroutine, it crashes the whole program and cannot be recovered from the parent.
解决方案
-
93% 成功率
go func() { defer func() { if r := recover(); r != nil { log.Printf("goroutine panic: %v", r) } }() doWork() }() -
90% 成功率
if cfg == nil { return errors.New("nil cfg") } go func(c Config) { use(c) }(*cfg)
无效尝试
常见但无效的做法:
-
95% 失败
recover only works in the panicking goroutine's own deferred function; the parent cannot catch it.
-
80% 失败
Race-dependent nil dereference only triggers under specific interleavings; production crashes intermittently.