go
runtime_error
ai_generated
true
panic:运行时错误:无效内存地址或空指针解引用
panic: runtime error: invalid memory address or nil pointer dereference
ID: go/goroutine-nil-pointer-deref
80%修复率
85%置信度
0证据数
2024-09-03首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.20 | active | — | — | — |
| 1.22 | active | — | — | — |
根因分析
新启动的 goroutine 解引用了空指针,常见原因是捕获了尚未初始化的变量,或在赋值前访问了结构体字段。
English
A spawned goroutine dereferences a nil pointer, often because it captures a variable that hasn't been initialized yet, or a struct field accessed before assignment.
解决方案
-
90% 成功率
Initialize all captured pointers before launching the goroutine, or pass values explicitly as function arguments: go func(cfg *Config) { ... }(cfg) -
88% 成功率
Use errgroup and return errors instead of panicking; validate pointer non-nil at goroutine entry.
无效尝试
常见但无效的做法:
-
70% 失败
Treats symptom; the goroutine still operates on invalid state and may corrupt downstream logic.
-
80% 失败
Hides the bug; other goroutines still crash and root cause is masked.