go runtime_error ai_generated true

panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV] (in goroutine N)

ID: go/nil-pointer-deref-goroutine

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

Version Compatibility

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

Root Cause

A goroutine dereferences a nil pointer, commonly a struct field or interface left uninitialized before the goroutine starts.

generic

中文

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

Workarounds

  1. 90% success
    Initialize all pointers before spawning the goroutine and prefer value receivers:
    
    cfg := &Config{}
    go worker(cfg) // cfg is non-nil
  2. 92% success
    Add nil guards at goroutine entry and return early with an error:
    
    if s == nil {
        return errors.New("nil service")
    }

Dead Ends

Common approaches that don't work:

  1. 80% fail

    The nil deref indicates a logic bug; swallowing it leaves the goroutine's work incomplete and can corrupt state.

  2. 95% fail

    Hides diagnostics; the crash still terminates the program.