我如何写一组地图[golang] [英] How can I write an array of maps [golang]

查看:130
本文介绍了我如何写一组地图[golang]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



示例:

  thisMap [coins] [0] = aMap [random:something] 
thisMap [notes] [1] = aMap [not-random别的东西]
thisMap [coins] [2] = aMap [not-random:something else]

我不知道如何做到这一点,因为 go 似乎只允许在处理 maps [name] [value] = value



到目前为止,我的代码失败了。 b
$ b

  package main 

func main(){

something:= [] string {硬币,笔记,金子,硬币,笔记}

thisMap:= make(map [string] [] map [string] int)

for k,v:=范围内的东西{
aMap:= map [string] string {
random:something,
}

thisMap [v] = [k] aMap
}
}

修改:切片值(硬币,笔记等)可以重复,所以这就是为什么我需要使用索引 [] 的原因。

解决方案

工作示例( click to )



  something:= [] string {coins,notes,gold? } 
$ b $ thisMap:= make(map [string] [] map [string] int)

for _,v:=范围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:=范围{
if _,ok: = thisMap [v]; !$ {
thisMap [v] = make(map [string] int)
}
thisMap [v] [random] = 12
}


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 [] .

解决方案

Working example (click to play):

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. Example with maps (click to play):

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
}

这篇关于我如何写一组地图[golang]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆