首页> 基础笔记 >GoLang基础学习 >数组 数组

Golang关联数组map的遍历操作

作者:小萝卜 2023-04-23 浏览 362

简介map遍历: 案例演示相对复杂的 map遍历:该 map的 value又是一个 map 说明:map的遍历使用 for-range的结构遍历

map遍历:

案例演示相对复杂的 map遍历:该 map的  value又是一个    map

说明:map的遍历使用  for-range的结构遍历

案例演示:

func main() {
	//使用for-range遍历map
	cities := make(map[string]string)
	cities["no1"] = "北京"
	cities["no2"] = "天津"
	cities["no3"] = "上海"
	
	for k, v := range cities {
		fmt.Printf("k=%v v=%v\n", k, v)
	}

	fmt.Println("cities 有", len(cities), " 对 key-value")

	//使用for-range遍历一个结构比较复杂的map
	studentMap := make(map[string]map[string]string)
	
	studentMap["stu01"] =  make(map[string]string, 3)
	studentMap["stu01"]["name"] = "tom"
	studentMap["stu01"]["sex"] = "男"
	studentMap["stu01"]["address"] = "北京长安街~"

	studentMap["stu02"] =  make(map[string]string, 3) //这句话不能少!!
	studentMap["stu02"]["name"] = "mary"
	studentMap["stu02"]["sex"] = "女"
	studentMap["stu02"]["address"] = "上海黄浦江~"

	for k1, v1 := range studentMap {
		fmt.Println("k1=", k1)
		for k2, v2 := range v1 {
				fmt.Printf("\t k2=%v v2=%v\n", k2, v2)
		}
		fmt.Println()
	}
}

很赞哦! (0)

文章评论

    高端网站建设