gpt4 book ai didi

go - 如何在编码而不是 json 时出错

转载 作者:行者123 更新时间:2023-12-03 10:10:11 25 4
gpt4 key购买 nike

我有一张 map ,其字符串和值的键是一个接口(interface)。我将一些键和值放入该映射中,我想对其进行编码,但我想获得“无效”,或者我希望我的编码出现错误?为了成功,我需要在该 map 中放入什么样的变量?

myBlobMap := make(map[string]interface{})
blobmap["firstKey"] = "firstValue"
blobmap["secondKey"] = "secondValue"
jsonByte, err := json.Marshal(myBlobMap)

最佳答案

来自 Go documentation on json :

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

JSON cannot represent cyclic data structures and Marshal does nothandle them. Passing cyclic structures to Marshal will result in anerror.


这是一个带有将返回错误的函数的示例:
package main

import (
"encoding/json"
"fmt"
)

func main() {
myBlobMap := make(map[string]interface{})
myBlobMap["firstKey"] = func() {}
myBlobMap["secondKey"] = "secondValue"
jsonByte, err := json.Marshal(myBlobMap)
fmt.Print(jsonByte, err)
}
Playground

关于go - 如何在编码而不是 json 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65133272/

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