go
type_error
ai_generated
true
json: 不支持的类型:chan int
json: unsupported type: chan int
ID: go/json-marshal-unsupported-type
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.
解决方案
-
90% 成功率
Use custom MarshalJSON method: func (c MyChan) MarshalJSON() ([]byte, error) { return json.Marshal("chan") } -
95% 成功率
Define struct with exported fields only: type S struct { Data string `json:"data"` }
无效尝试
常见但无效的做法:
-
60% 失败
Data loss; field missing.
-
80% 失败
Channels can't be converted; type mismatch.