英文:
How to reset value in map of maps
问题
这是我的代码:
{
saruman := map[string]int{
"power": 150,
"skills": 200,
"knowledge": 380,
}
gandalf := map[string]int{
"power": 180,
"skills": 210,
"knowledge": 240,
}
wizards := make(map[string]interface{})
wizards["saruman"] = saruman
wizards["gandalf"] = gandalf
fmt.Println(wizards)
// 输出:map[gandalf:map[knowledge:240 power:180 skills:210] saruman:map[knowledge:380 power:150 skills:200]]
// 修改 gandalf 的属性
wizards["gandalf"]["knowledge"] = 310
fmt.Println(wizards)
}
在最后一行,我们得到了以下错误信息:
NOK 无效操作:wizards["gandalf"]["knowledge"](类型 interface {} 不支持索引操作)
英文:
Here's my code
{
saruman := map[string]int{
"power": 150,
"skills": 200,
"knowledge": 380,
}
gandalf := map[string]int{
"power": 180,
"skills": 210,
"knowledge": 240,
}
wizards := make(map[string]interface{})
wizards["saruman"] = saruman
wizards["gandalf"] = gandalf
fmt.Println(wizards)
//OK map[gandalf:map[knowledge:240 power:180 skills:210] saruman:map[knowledge:380 power:150 skills:200]]
//change gandalf property
wizards["gandalf"]["knowledge"] = 310
fmt.Println(wizards)
}
In the last line, we get:
> NOK invalid operation: wizards["gandalf"]["knowledge"] (type interface {} does not support indexing)
答案1
得分: 1
你不能使用["key"]
来访问golang不知道的结构。因为它是一个interface{}
,gandalf后面的值可以是任何类型。
你必须指定你正在交互的数据类型。
Playground: https://goplay.tools/snippet/O62ara8JbUv
英文:
You cannot use ["key"]
to access a structure that golang does not know about. Because it is an interface{}
the value behind gandalf could be anything.
wizards["gandalf"] = map[string]int{
"knowledge": 310,
}
You have to specify the type of data you are interacting with.
Playground: https://goplay.tools/snippet/O62ara8JbUv
答案2
得分: 0
需要进行类型转换。
{
saruman := map[string]int{
"power": 150,
"skills": 200,
"knowledge": 380,
}
gandalf := map[string]int{
"power": 180,
"skills": 210,
"knowledge": 240,
}
wizards := make(map[string]interface{})
wizards["saruman"] = saruman
wizards["gandalf"] = gandalf
fmt.Println(wizards)
// 输出:map[gandalf:map[knowledge:240 power:180 skills:210] saruman:map[knowledge:380 power:150 skills:200]]
// 修改 gandalf 的属性
wizards["gandalf"].(map[string]int)["knowledge"] = 10
fmt.Println(wizards)
}
以上是代码的翻译结果。
英文:
Need to cast it.
{
saruman := map[string]int{
"power": 150,
"skills": 200,
"knowledge": 380,
}
gandalf := map[string]int{
"power": 180,
"skills": 210,
"knowledge": 240,
}
wizards := make(map[string]interface{})
wizards["saruman"] = saruman
wizards["gandalf"] = gandalf
fmt.Println(wizards)
//OK map[gandalf:map[knowledge:240 power:180 skills:210] saruman:map[knowledge:380 power:150 skills:200]]
//change gandalf property
wizards["gandalf"].(map[string]int)["knowledge"] = 10
fmt.Println(wizards)
}
答案3
得分: 0
这是更简单的方法:
一行代码
wizards["gandalf"].(map[string]int)["knowledge"] = 110
完整示例:
saruman := map[string]int{
"power": 150,
"skills": 200,
"knowledge": 380,
}
gandalf := map[string]int{
"power": 180,
"skills": 210,
"knowledge": 240,
}
wizards := make(map[string]interface{})
wizards["saruman"] = saruman
wizards["gandalf"] = gandalf
// fmt.Println(wizards)
// OK map[gandalf:map[knowledge:240 power:180 skills:210] saruman:map[knowledge:380 power:150 skills:200]]
// 修改gandalf的属性
wizards["gandalf"].(map[string]int)["knowledge"] = 110
fmt.Println(wizards["gandalf"].(map[string]int)["knowledge"])
英文:
This is the easier way:<br>
one line code
wizards["gandalf"].(map[string]int)["knowledge"] = 110
Full example:
saruman := map[string]int{
"power": 150,
"skills": 200,
"knowledge": 380,
}
gandalf := map[string]int{
"power": 180,
"skills": 210,
"knowledge": 240,
}
wizards := make(map[string]interface{})
wizards["saruman"] = saruman
wizards["gandalf"] = gandalf
// fmt.Println(wizards)
// OK map[gandalf:map[knowledge:240 power:180 skills:210] saruman:map[knowledge:380 power:150 skills:200]]
// change gandalf property
wizards["gandalf"].(map[string]int)["knowledge"] = 110
fmt.Println(wizards["gandalf"].(map[string]int)["knowledge"])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论