golang map的遍历

前端之家收集整理的这篇文章主要介绍了golang map的遍历前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

遍历key

package main


import (
        "fmt"
)


func main() {
        var mymap map[string]string

        mymap = map[string]string{"1a": "Very","2b": "good","3c": "day"}

        for one := range mymap {
                fmt.Println(one)
        }

}

output:

1a
2b
3c

遍历value

package main


import (
        "fmt"
)


func main() {
        var mymap map[string]string

        mymap = map[string]string{"1a": "Very","3c": "day"}

        for _,value := range mymap {
                fmt.Println(value)
        }

}

output:

Very
good
day

遍历key-value

package main


import (
        "fmt"
)


func main() {
        var mymap map[string]string

        mymap = map[string]string{"1a": "Very","3c": "day"}

        for key,value := range mymap {
                fmt.Println(key,":",value)
        }

}

output:

3c : day 1a : Very 2b : good

猜你在找的Go相关文章