英文:
How to add data in a nested map?
问题
我有一个嵌套的映射数据,其中一个是列表,我想使用append方法向列表中添加数据,但是返回了一个错误:
invalid argument: baseData["a"].(map[string]interface{})["actions"] (map index expression of type interface{}) is not a slice
以下是你的代码:
baseData := map[string]interface{}{}
dateData := map[string]interface{}{
"date": map[string]interface{}{
"updatedAt": "yyyy-mm-dd",
"isChanged": true,
},
"actions": []interface{}{},
}
baseData["a"] = dateData
.....示例
{
"a": {
"actions": [],
"date": {
"isChanged": true,
"updatedAt": "yyyy-mm-dd"
}
},
"b": {
"actions": [],
"date": {
"isChanged": true,
"updatedAt": "yyyy-mm-dd"
}
}
}
我的代码:
actionData := map[string]interface{}{
"id": data.Id,
"action": data.Action,
}
baseData["a"].(map[string]interface{})["actions"] = append(baseData["a"].(map[string]interface{})["actions"], actionData)
希望这可以帮助到你!
英文:
I have a nested map data that
One of them is a list, and I want to add data to the list using the append method
but it return an error :
invalid argument: baseData["a"].(map[string]interface{})["actions"] (map index expression of type interface{}) is not a slice
baseData := map[string]interface{}{}
dateData := map[string]interface{}{
"date": map[string]interface{}{
"updatedAt": "yyyy-mm-dd",
"isChanged": true,
},
"actions": []interface{}{},
}
baseData["a"] = dateData
.....example
{
"a": {
"actions": [],
"date": {
"isChanged": true,
"updatedAt": "yyyy-mm-dd"
}
},
"b": {
"actions": [],
"date": {
"isChanged": true,
"updatedAt": "yyyy-mm-dd"
}
}
}
My codes
actionData := map[string]interface{}{
"id": data.Id,
"action": data.Action,
}
baseData["a"].(map[string]interface{})["actions"] = append(baseData["a"].(map[string]interface{})["actions"], actionData)
答案1
得分: 1
做这个:
package main
import "fmt"
func main() {
dateData := map[string]interface{}{
"actions": []interface{}{},
}
dateData["actions"] = append(dateData["actions"].([]interface{}), "foobar")
fmt.Printf("%+v\n", dateData)
}
输出:
map[actions:[foobar]]
英文:
Do this:
package main
import "fmt"
func main() {
dateData := map[string]interface{}{
"actions": []interface{}{},
}
dateData["actions"] = append(dateData["actions"].([]interface{}), "foobar")
fmt.Printf("%+v\n", dateData)
}
Output:
map[actions:[foobar]]
答案2
得分: 0
baseData := map[string]interface{}{}
dateData := map[string]interface{}{
"date": map[string]interface{}{
"updatedAt": "yyyy-mm-dd",
"isChanged": true,
},
"actions": []interface{}{},
}
baseData["a"] = dateData
actionData := map[string]interface{}{
"id": data.Id,
"action": data.Action,
}
baseData["a"].(map[string]interface{})["actions"] = append(baseData["a"].(map[string]interface{})["actions"].([]interface{}), actionData)
英文:
baseData := map[string]interface{}{}
dateData := map[string]interface{}{
"date": map[string]interface{}{
"updatedAt": "yyyy-mm-dd",
"isChanged": true,
},
"actions": []interface{}{},
}
baseData["a"] = dateData
actionData := map[string]interface{}{
"id": data.Id,
"action": data.Action,
}
baseData["a"].(map[string]interface{})["actions"] = append(baseData["a"].(map[string]interface{})["actions"].([]interface{}), actionData)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论