go type_error ai_generated true

panic: json: unsupported type: chan int

ID: go/json-marshal-channels

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active

Root Cause

尝试将channel或函数等不支持的类型序列化为JSON。

generic

中文

尝试将channel或函数等不支持的类型序列化为JSON。

Workarounds

  1. 95% success
    在结构体中使用`json:"-"`忽略字段:
    type Message struct {
        Ch chan int `json:"-"`
        Data string `json:"data"`
    }
  2. 80% success
    实现MarshalJSON方法自定义序列化。

Dead Ends

Common approaches that don't work:

  1. 100% fail

    程序崩溃。

  2. 90% fail

    json.Marshal不处理channel。