以下是一个示例: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--"}
切片?我基本上需要创建任意数据结构,将其转换为json。查看https://github.com/Jeffail/gabs,但它似乎不允许创建嵌套数组
答案 0 :(得分:2)
这有点乱,但你可以按照这里所示的那样做(我遗漏了错误处理):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)