go runtime_error ai_generated true

panic: 运行时错误:无效内存地址或 nil 指针解引用 [signal SIGSEGV](在 goroutine N 中)

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

ID: go/nil-pointer-deref-goroutine

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2024-10-05首次发现

版本兼容性

版本状态引入弃用备注
1.18 active — — —
1.21 active — — —
1.22 active — — —

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 80% 失败

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

  2. 95% 失败

    Hides diagnostics; the crash still terminates the program.