一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!


可以使用for range循环进行map遍历,得到key和value值。

遍历key
package main

import "fmt"

func main() {
    m := make(map[string]string)
    m["name"] = "tom"
    m["age"] = "20"
    m["email"] = "tom@gmail.com"

    for key := range m {
        fmt.Println(key)
    }
}

运行结果

[Running] go run "d:\SynologyDrive\软件开发\go\golang入门到项目实战\goproject\360duote.com\pro01\test.go"
name
age
email
遍历key和value
package main

import "fmt"

func main() {
    m := make(map[string]string)
    m["name"] = "tom"
    m["age"] = "20"
    m["email"] = "tom@gmail.com"

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

运行结果

[Running] go run "d:\SynologyDrive\软件开发\go\golang入门到项目实战\goproject\360duote.com\pro01\test.go"
name:tom
age:20
email:tom@gmail.com