go
runtime_error
ai_generated
true
panic: runtime error: invalid memory address or nil pointer dereference (in goroutine)
ID: go/goroutine-nil-pointer
80%Fix Rate
86%Confidence
0Evidence
2024-07-02First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.20 | active | — | — | — |
| 1.22 | active | — | — | — |
Root Cause
A nil pointer is dereferenced inside a spawned goroutine. Because the panic happens in a child goroutine, it crashes the whole program and cannot be recovered from the parent.
generic中文
在派生的 goroutine 中解引用了空指针。由于 panic 发生在子 goroutine 中,会导致整个程序崩溃,父 goroutine 无法 recover。
Workarounds
-
93% success
go func() { defer func() { if r := recover(); r != nil { log.Printf("goroutine panic: %v", r) } }() doWork() }() -
90% success
if cfg == nil { return errors.New("nil cfg") } go func(c Config) { use(c) }(*cfg)
Dead Ends
Common approaches that don't work:
-
95% fail
recover only works in the panicking goroutine's own deferred function; the parent cannot catch it.
-
80% fail
Race-dependent nil dereference only triggers under specific interleavings; production crashes intermittently.