go runtime_error ai_generated true

panic: json: calling MarshalJSON for type main.MyType with non-addressable value

ID: go/encoding-json-marshal-non-addressable-value

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.20 active
1.21 active

Root Cause

Attempting to marshal a value that is not addressable, such as a map key, which cannot have its method called.

generic

中文

尝试编组一个不可寻址的值,例如映射键,无法调用其方法。

Workarounds

  1. 90% success Ensure MarshalJSON has pointer receiver and pass pointer
    func (m *MyType) MarshalJSON() ([]byte, error) { ... }
    // Always pass pointer
    json.Marshal(&myValue)

Dead Ends

Common approaches that don't work:

  1. Using reflect to make it addressable 70% fail

    Complex and may panic; better to use pointer receiver.