go
data_error
ai_generated
true
json: 调用 MyStruct 类型的 MarshalJSON 出错:自定义错误
json: error calling MarshalJSON for type MyStruct: custom error
ID: go/encoding-json-marshaler-error
80%修复率
81%置信度
0证据数
2024-05-25首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.20 | active | — | — | — |
根因分析
自定义 MarshalJSON 方法返回错误,导致 json.Marshal 失败。
English
A custom MarshalJSON method returns an error, causing json.Marshal to fail.
解决方案
-
85% 成功率
func (m MyStruct) MarshalJSON() ([]byte, error) { if m.Invalid { return nil, errors.New("invalid state") }; return json.Marshal(m.Alias) }
无效尝试
常见但无效的做法:
-
70% 失败
Loses custom serialization logic, may produce wrong output.
-
90% 失败
Panic propagates and crashes the program.