1.golang map数据结构

package main

import (
“fmt”
)

type Stu struct {
name string
age int
grade float64
}

func main() {
//map是引用类型,遵守引用类型值传递的机制,在一个函数中接受map,修改后会直接修改原来的map
//map的容量满了之后,map会自动扩容,不会发生panic,map能动态增长键值对
num := make(map[int]int, 1) //容量为1,自动扩容

num[1] = 10
num[5] = 20
num[9] = 30
modify(num) //调用函数修改num[5]值
fmt.Println(num)

//map的value也经常使用struct类型,更适合管理复杂数据
student := make(map[string]Stu, 10)
stu1 := Stu{“star gazer”, 25, 100}
stu2 := Stu{“ali”, 24, 97}
stu3 := Stu{“zoe”, 5000, 91}
stu4 := Stu{“alex”, 30, 60}
stu5 := Stu{“arlia”, 27, 80}

student[“no1”] = stu1
student[“no2”] = stu2
student[“no3”] = stu3
student[“no4”] = stu4
student[“no5”] = stu5

fmt.Println(student)

//遍历学生信息
for k, v := range student {
fmt.Printf(“%v 号学生的名字是 %v\n”, k, v.name)
fmt.Printf(“%v 号学生的年龄是 %v\n”, k, v.age)
fmt.Printf(“%v 号学生的分数是 %v\n”, k, v.grade)
}
}
func modify(num map[int]int) {

num[5] = 40
}

2.golang map底层原理

原文链接:https://www.cnblogs.com/PatrickStarGazer/p/15972042.html