go type_error ai_generated true

json: unsupported type: chan int

ID: go/json-marshal-unsupported-type

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-12-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.21 active

Root Cause

Attempting to marshal a Go type that is not supported by encoding/json, such as channels, functions, or complex numbers.

generic

中文

尝试序列化 encoding/json 不支持的 Go 类型,如通道、函数或复数。

Workarounds

  1. 90% success
    Use custom MarshalJSON method: func (c MyChan) MarshalJSON() ([]byte, error) { return json.Marshal("chan") }
  2. 95% success
    Define struct with exported fields only: type S struct { Data string `json:"data"` }

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Data loss; field missing.

  2. 80% fail

    Channels can't be converted; type mismatch.