go
runtime_error
ai_generated
true
错误:json:Unmarshal(nil *main.MyStruct)
error: json: Unmarshal(nil *main.MyStruct)
ID: go/encoding-json-unmarshal-nil-pointer
80%修复率
90%置信度
0证据数
2025-03-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.20 | active | — | — | — |
| 1.21 | active | — | — | — |
根因分析
向json.Unmarshal传递nil指针,无法将数据解组到nil目标。
English
Passing a nil pointer to json.Unmarshal, which cannot unmarshal into a nil target.
解决方案
-
98% 成功率 Always initialize the target variable before unmarshaling
var myStruct MyStruct if err := json.Unmarshal(data, &myStruct); err != nil { return err }
无效尝试
常见但无效的做法:
-
Ignoring nil check before unmarshaling
90% 失败
Leads to panic; always initialize target variable.