我想创建具有动态数据结构的结果对象 json,ex 在 func1 中,结果是这样的
{
'result': 'this is result',
'content': {
'func1' : 'value',
'some_desc_func1': 'value'
}
}
和 func2 可能结果只是(专注于内容)像这样
{
'result': 'this is result',
'content': {
'func2' : 'value'
}
}
作为引用 https://stackoverflow.com/a/35657622/4476788 ,我想用结果的一个键显示结果 json。
像这样
{
'result': 'this is result',
'content': {
'key' : 'value'
}
}
不是这样的
[
{
'result_1' : 'answer 1'
},
{
'result_2' : 'answer 2'
}
]
我尝试更新答案的 play ground 但它显示错误
type Brand struct {
Name string
}
var database map[string]interface{}
func init() {
database = make(map[string]interface{})
brands := make([]Brand, 1)
brands = Brand{"Gucci"}
database["brands"] = brands
}
你可以尝试在这里运行 https://play.golang.org/p/mKCwKEVI7E
显示错误
tmp/sandbox651609402/main.go:22:不能使用品牌字面量(品牌类型)作为赋值中的类型[]品牌
最佳答案
第 22 行应该是:brands = []Brand{Brand{"Gucci"}}
关于json - Go语言中带有结果对象的动态结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41054288/