go type_error ai_generated true

json: 不支持的类型:chan int

json: unsupported type: chan int

ID: go/json-marshal-unsupported-type

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-12-10首次发现

版本兼容性

版本状态引入弃用备注
1.21 active

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 60% 失败

    Data loss; field missing.

  2. 80% 失败

    Channels can't be converted; type mismatch.