go
runtime_error
ai_generated
true
无效的内存地址或空指针解引用 [信号 SIGSEGV: 分段违规]
invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation]
ID: go/invalid-memory-address-or-nil-pointer-dereference-in-select
90%修复率
86%置信度
1证据数
2023-08-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| go1.18 | active | — | — | — |
| go1.19 | active | — | — | — |
| go1.20 | active | — | — | — |
| go1.21 | active | — | — | — |
根因分析
在 goroutine 中解引用空指针,通常发生在 select 语句或通道操作中,指针未正确初始化。
English
Dereferencing a nil pointer in a goroutine, often inside a select statement or channel operation where a pointer was not properly initialized.
官方文档
https://go.dev/doc/faq#nil_error解决方案
-
Add explicit nil checks before dereferencing: `if ptr != nil { ptr.Field = value } else { log.Fatal("ptr is nil") }` -
Initialize all pointers with make or new before use: `ptr = new(StructType)` or `ptr = &StructType{}`
无效尝试
常见但无效的做法:
-
90% 失败
SIGSEGV is a signal, not a panic; recover() cannot catch segmentation violations—the program still crashes.
-
70% 失败
Nil pointer dereference is not a race condition; the pointer is nil regardless of synchronization.