go runtime_error ai_generated true

panic: runtime error: invalid memory address or nil pointer dereference (in goroutine)

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

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0+ active

Root Cause

A goroutine dereferences a nil pointer; because it runs in a background goroutine, the panic crashes the whole program.

generic

中文

某个 goroutine 解引用 nil 指针;由于运行在后台 goroutine 中,panic 会导致整个程序崩溃。

Workarounds

  1. 90% success
    go func() {
        defer func() {
            if r := recover(); r != nil {
                log.Printf("panic: %v", r)
            }
        }()
        doWork()
    }()
  2. 93% success
    if p == nil {
        return fmt.Errorf("nil receiver")
    }

Dead Ends

Common approaches that don't work:

  1. 95% fail

    Panic happens in a child goroutine; recover in main never sees it.

  2. 90% fail

    Any unhandled panic in a goroutine terminates the process.