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...] (in goroutine N)

ID: go/goroutine-nil-pointer-in-goroutine

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

Version Compatibility

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

Root Cause

A goroutine dereferences a nil pointer, often because it captured a variable that was not yet assigned or a struct field was left uninitialized.

generic

中文

goroutine 解引用了 nil 指针,通常是因为它捕获了尚未赋值的变量,或结构体字段未初始化。

Workarounds

  1. 90% success
    for _, item := range items {
        item := item
        go func(it *Item) {
            process(it)
        }(&item)
    }
  2. 92% success
    svc := &Service{Client: newClient()}
    go svc.Run()

Dead Ends

Common approaches that don't work:

  1. 60% fail

    If the nil is a logic error (e.g., missing initialization), skipping the work silently hides the bug.

  2. 80% fail

    Recovering prevents a crash but does not fix the root cause; the same nil deref will recur.