go
runtime_error
ai_generated
true
error: json: Unmarshal(nil *main.MyStruct)
ID: go/encoding-json-unmarshal-nil-pointer
80%Fix Rate
90%Confidence
0Evidence
2025-03-05First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.20 | active | — | — | — |
| 1.21 | active | — | — | — |
Root Cause
Passing a nil pointer to json.Unmarshal, which cannot unmarshal into a nil target.
generic中文
向json.Unmarshal传递nil指针,无法将数据解组到nil目标。
Workarounds
-
98% success Always initialize the target variable before unmarshaling
var myStruct MyStruct if err := json.Unmarshal(data, &myStruct); err != nil { return err }
Dead Ends
Common approaches that don't work:
-
Ignoring nil check before unmarshaling
90% fail
Leads to panic; always initialize target variable.