gpt4 book ai didi

json:不支持的类型:func()

转载 作者:行者123 更新时间:2023-12-01 22:34:46 27 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





What input will cause golang's json.Marshal to return an error?

(4 个回答)


2年前关闭。




除了 Go 中的函数,还有其他类型不能使用 json.Marhsal 编码吗?

如果我将 func 传递给 json.Marshal,我会得到:

json: unsupported type: func()

Golang 中是否还有其他无法序列化/编码的数据类型?我正在编写一个迷你库,想知道是否需要检查其他无法序列化的内容。

最佳答案

来自 json.Marshal() :

Channel, complex, and function values cannot be encoded in JSON. Attempting to encode such a value causes Marshal to return an UnsupportedTypeError.

JSON cannot represent cyclic data structures and Marshal does not handle them. Passing cyclic structures to Marshal will result in an infinite recursion.


更进一步,文档中没有提到,而是 unsafe.Pointer也不能编码。
任何包含不受支持类型的复合类型也是如此(例如 channel slice 、保存 unsafe.Pointer 导出字段的结构等)。

关于json:不支持的类型:func(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60203395/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com