go type_error ai_generated true

error: json: unsupported type: chan int

ID: go/json-marshal-unsupported-type

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.19 active

Root Cause

尝试将 Go 中不支持 JSON 序列化的类型(如 channel、complex、func)传递给 json.Marshal。

generic

中文

尝试将 Go 中不支持 JSON 序列化的类型(如 channel、complex、func)传递给 json.Marshal。

Workarounds

  1. 90% success
    func (c MyChan) MarshalJSON() ([]byte, error) { return json.Marshal(c.String()) }
  2. 85% success
    type MyStruct struct { Data []int `json:"data"` }

Dead Ends

Common approaches that don't work:

  1. 80% fail

    会丢失类型信息,且无法还原为原始数据。

  2. 60% fail

    如果该字段是必须的,忽略会导致数据不完整。