go
runtime_error
ai_generated
true
invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation]
ID: go/invalid-memory-address-or-nil-pointer-dereference-in-select
90%Fix Rate
86%Confidence
1Evidence
2023-08-05First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| go1.18 | active | — | — | — |
| go1.19 | active | — | — | — |
| go1.20 | active | — | — | — |
| go1.21 | active | — | — | — |
Root Cause
Dereferencing a nil pointer in a goroutine, often inside a select statement or channel operation where a pointer was not properly initialized.
generic中文
在 goroutine 中解引用空指针,通常发生在 select 语句或通道操作中,指针未正确初始化。
Official Documentation
https://go.dev/doc/faq#nil_errorWorkarounds
-
95% success Add explicit nil checks before dereferencing: `if ptr != nil { ptr.Field = value } else { log.Fatal("ptr is nil") }`
Add explicit nil checks before dereferencing: `if ptr != nil { ptr.Field = value } else { log.Fatal("ptr is nil") }` -
90% success Initialize all pointers with make or new before use: `ptr = new(StructType)` or `ptr = &StructType{}`
Initialize all pointers with make or new before use: `ptr = new(StructType)` or `ptr = &StructType{}`
中文步骤
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{}`
Dead Ends
Common approaches that don't work:
-
90% fail
SIGSEGV is a signal, not a panic; recover() cannot catch segmentation violations—the program still crashes.
-
70% fail
Nil pointer dereference is not a race condition; the pointer is nil regardless of synchronization.