gpt4 book ai didi

go - 如何将值推送到嵌套的 map[string]interface{} slice 中?

转载 作者:数据小太阳 更新时间:2023-10-29 03:33:05 28 4
gpt4 key购买 nike

这是一个例子:https://play.golang.org/p/aQXJzH6Yjo

i := make(map[string]interface{})
i["some"] = []interface{}{
[]interface{}{1, 2, "3--"},
map[string]interface{}{
"value": "some",
},
}

如何将附加值推送到 []interface{}{1, 2, "3--"} slice ?我基本上需要创建将转换为 json 的任意数据结构。调查了https://github.com/Jeffail/gabs , 但它似乎不允许创建嵌套数组

最佳答案

有点乱,但您可以按照此处所示进行操作(我省略了错误处理):https://play.golang.org/p/JgZ4fAgRAz

i := make(map[string]interface{})
i["some"] = []interface{}{
[]interface{}{1, 2, "3--"},
map[string]interface{}{
"value": "some",
},
}
fmt.Println(i)
var myval []interface{} = i["some"].([]interface{})
var mylist []interface{} = myval[0].([]interface{})

mylist = append (mylist, "Another value")

// Replace the potentially new slice into element 0
myval[0] = mylist

// No need to write back to the map - the slice is a reference type
fmt.Println(i)

关于go - 如何将值推送到嵌套的 map[string]interface{} slice 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42733908/

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