英文:
How can I write an array of maps [golang]
问题
我有一个值为数组的地图。
示例:
thisMap["coins"][0] = aMap["random":"something"]
thisMap["notes"][1] = aMap["not-random":"something else"]
thisMap["coins"][2] = aMap["not-random":"something else"]
我无法弄清楚如何以 go
的方式实现这一点,因为当你处理 maps [name][value] = value
时,它似乎只允许在一个级别上设置数据。
到目前为止,我有以下代码,但失败了
package main
func main() {
something := []string{"coins", "notes", "gold?", "coins", "notes"}
thisMap := make(map[string][]map[string]int)
for k, v := range something {
aMap := map[string]string{
"random": "something",
}
thisMap[v] = [k]aMap
}
}
编辑:切片值("coins","notes"等)可以重复,这就是为什么我需要使用索引 []
的原因。
英文:
I have a map which has as value an array of maps.
Example:
thisMap["coins"][0] = aMap["random":"something"]
thisMap["notes"][1] = aMap["not-random":"something else"]
thisMap["coins"][2] = aMap["not-random":"something else"]
I can't figure it out how to do this as go
seems to allow setting data only at one level when you deal with maps [name][value] = value
.
So far I have this code which fails
package main
func main() {
something := []string{"coins", "notes", "gold?", "coins", "notes"}
thisMap := make(map[string][]map[string]int)
for k, v := range something {
aMap := map[string]string{
"random": "something",
}
thisMap[v] = [k]aMap
}
}
Edit: The slice values ("coins", "notes" etc ) can repeat so this is the reason why I need use an index []
.
答案1
得分: 26
工作示例(点击播放):
something := []string{"coins", "notes", "gold?"}
thisMap := make(map[string][]map[string]int)
for _, v := range something {
aMap := map[string]int{
"random": 12,
}
thisMap[v] = append(thisMap[v], aMap)
}
在对新创建的thisMap
进行迭代时,您需要为新值aMap
腾出空间。使用切片时,内置函数append
会为您腾出空间并将值附加到切片中。
如果您使用的是无法像切片那样轻松初始化的更复杂的数据类型,您首先需要检查键是否已经存在于映射中,如果不存在,则初始化您的数据类型。有关检查映射元素的信息,请参阅此处的文档。使用映射的示例(点击播放):
thisMap := make(map[string]map[string]int)
for _, v := range something {
if _, ok := thisMap[v]; !ok {
thisMap[v] = make(map[string]int)
}
thisMap[v]["random"] = 12
}
以上是您提供的代码的翻译结果。
<details>
<summary>英文:</summary>
Working example ([click to play][1]):
something := []string{"coins", "notes", "gold?"}
thisMap := make(map[string][]map[string]int)
for _, v := range something {
aMap := map[string]int{
"random": 12,
}
thisMap[v] = append(thisMap[v], aMap)
}
When iterating over the newly created `thisMap`, you need to make room for the new value `aMap`. The builtin function `append` does that for you when using slices. It makes room and appends the value to the slice.
If you're using more complex data types that can't be initialized as easily as slices, you first have to check if the key is already in the map and, if it is not, initialize your data type. Checking for map elements is documented [here][2]. Example with maps ([click to play][3]):
thisMap := make(map[string]map[string]int)
for _, v := range something {
if _, ok := thisMap[v]; !ok {
thisMap[v] = make(map[string]int)
}
thisMap[v]["random"] = 12
}
[2]: http://golang.org/ref/spec#Index_expressions
[1]: http://play.golang.org/p/p5e5a2Czh7
[3]: http://play.golang.org/p/RAXhRDNN-x
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论