go runtime_error ai_generated true

panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x...]

ID: go/panic-runtime-error-invalid-memory-address-or-nil-pointer-

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-10-04First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.21 active — — —
1.22 active — — —

Root Cause

A goroutine dereferences a nil pointer, often a struct field or interface that was never initialized before the goroutine started.

generic

中文

某个 goroutine 解引用了 nil 指针,通常是 goroutine 启动前未初始化的结构体字段或接口。

Workarounds

  1. 92% success
    Initialize all shared pointers before launching goroutines, and capture them as arguments: go worker(cfg) where cfg is non-nil.
  2. 85% success
    Add explicit nil checks at goroutine entry: if cfg == nil { log.Fatal("cfg is nil") }.

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Recover in main does not catch panics in other goroutines; each goroutine needs its own recover, and hiding the panic masks the bug.

  2. 75% fail

    Creates a zero-valued object disconnected from the shared state the caller expected, causing silent logic errors.