go runtime_error ai_generated true

error: json: Unmarshal(nil *main.MyStruct)

ID: go/encoding-json-unmarshal-nil-pointer

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

  1. 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:

  1. Ignoring nil check before unmarshaling 90% fail

    Leads to panic; always initialize target variable.