遍历一个 golang 的 map

huangapple go评论87阅读模式
英文:

Iterating through a golang map

问题

我有一个类型为map[string]interface{}的地图。

最后,我得到了创建类似以下内容的东西(在使用goyaml从yml文件反序列化之后):

mymap = map[foo:map[first: 1] boo: map[second: 2]]

我该如何遍历这个地图?我尝试了以下方法:

for k, v := range mymap{
...
}

但是我得到了一个错误:

无法遍历mymap
循环类型检查涉及for循环

请帮忙。

英文:

I have a map of type: map[string]interface{}

And finally, I get to create something like (after deserializing from a yml file using goyaml)

mymap = map[foo:map[first: 1] boo: map[second: 2]]

How can I iterate through this map? I tried the following:

for k, v := range mymap{
...
}

But I get an error:

cannot range over mymap
typechecking loop involving for loop

Please help.

答案1

得分: 131

例如,

package main

import "fmt"

func main() {
    type Map1 map[string]interface{}
    type Map2 map[string]int
    m := Map1{"foo": Map2{"first": 1}, "boo": Map2{"second": 2}}
    //m = map[foo:map[first: 1] boo: map[second: 2]]
    fmt.Println("m:", m)
    for k, v := range m {
        fmt.Println("k:", k, "v:", v)
    }
}

输出:

m: map[boo:map[second:2] foo:map[first:1]]
k: boo v: map[second:2]
k: foo v: map[first:1]
英文:

For example,

package main

import "fmt"

func main() {
	type Map1 map[string]interface{}
	type Map2 map[string]int
	m := Map1{"foo": Map2{"first": 1}, "boo": Map2{"second": 2}}
	//m = map[foo:map[first: 1] boo: map[second: 2]]
	fmt.Println("m:", m)
	for k, v := range m {
		fmt.Println("k:", k, "v:", v)
	}
}

Output:

m: map[boo:map[second:2] foo:map[first:1]]
k: boo v: map[second:2]
k: foo v: map[first:1]

答案2

得分: 5

你可以像这样将其写成多行形式,

$ cat dict.go
package main

import "fmt"

func main() {
        items := map[string]interface{}{
                "foo": map[string]int{
                        "strength": 10,
                        "age": 2000,
                },
                "bar": map[string]int{
                        "strength": 20,
                        "age": 1000,
                },
        }
        for key, value := range items {
                fmt.Println("[", key, "] has items:")
                for k,v := range value.(map[string]int) {
                        fmt.Println("\t-->", k, ":", v)
                }

        }
}

输出结果为:

$ go run dict.go
[ foo ] has items:
        --> strength : 10
        --> age : 2000
[ bar ] has items:
        --> strength : 20
        --> age : 1000
英文:

You could just write it out in multiline like this,

$ cat dict.go
package main

import "fmt"

func main() {
        items := map[string]interface{}{
                "foo": map[string]int{
                        "strength": 10,
                        "age": 2000,
                },
                "bar": map[string]int{
                        "strength": 20,
                        "age": 1000,
                },
        }
        for key, value := range items {
                fmt.Println("[", key, "] has items:")
                for k,v := range value.(map[string]int) {
                        fmt.Println("\t-->", k, ":", v)
                }

        }
}

And the output:

$ go run dict.go
[ foo ] has items:
        --> strength : 10
        --> age : 2000
[ bar ] has items:
        --> strength : 20
        --> age : 1000

答案3

得分: 3

mymap := map[string]interface{}{"foo": map[string]interface{}{"first": 1}, "boo": map[string]interface{}{"second": 2}}
for k, v := range mymap {
fmt.Println("k:", k, "v:", v)
}

英文:

You can make it by one line:

mymap := map[string]interface{}{"foo": map[string]interface{}{"first": 1}, "boo": map[string]interface{}{"second": 2}}
for k, v := range mymap {
	fmt.Println("k:", k, "v:", v)
}

Output is:

k: foo v: map[first:1]
k: boo v: map[second:2]

huangapple
  • 本文由 发表于 2011年11月5日 15:03:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/8018719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定