map
- map的声明不会分配内存,而数组一旦声明即分配内存。
var map1 map[string]string
var map2 map[string]map[string]string
故声明后必须make,给map分配数据空间 - map的key重复后将会被覆盖
- map是无序的
map的声明
package main
import(
"fmt"
)
func main(){
// 声明方式1
var map1 map[string]string
map1 = make(map[string]string,10) // 必须分配数据空间
map1["lwt"]="666" // 再赋值
fmt.Println(map1)
// 声明方式2
var map2 map[string]string = make(map[string]string,10)
map2["lwt"]="777"
fmt.Println(map2)
// 声明方式3
var map3 = make(map[string]string,10)
map3["lwt"]="888"
fmt.Println(map3)
// 声明方式4(直接赋值)未使用到make,但直接花括号赋值
var map4 = map[string]string{
"lwt4":"666",
"lwt4-1":"666", // 这里的逗号不能省略
}
fmt.Println(map4)
fmt.Println(map4["lwt4"]) //取map的值,666
// 推荐方式5
map5 := make(map[string]string)
map5["lwt5"]="666"
fmt.Println(map5)
StuMap := make(map[string]map[string]string)
StuMap["学生1"] = make(map[string]string,2)
StuMap["学生1"]["name"] = "李威彤1"
StuMap["学生1"]["sex"] = "男"
// map嵌套后,每次都要make
StuMap["学生2"] = make(map[string]string,2)
StuMap["学生2"]["name"] = "李威彤2"
StuMap["学生2"]["sex"] = "女"
fmt.Println(StuMap)
fmt.Println(StuMap["学生2"]["name"])
// 法1式删除
StuMap = make(map[string]map[string]string)
fmt.Println("将StuMap重新声明,即删了(法一)",StuMap)
// 法2式遍历删除
// map查找
val,ok := map4["lwt4"] // 返回的ok为布尔值
if ok{
fmt.Println("有key,值为:",val)
}else{
fmt.Println("滚,啥都没有")
}
}
map的遍历(for-range)
package main
import(
"fmt"
)
func main(){
map1 := make(map[string]string)
map1["lwt1"]="11"
map1["lwt2"]="22"
map1["lwt3"]="33"
map1["lwt4"]="44"
for key,value := range(map1){
fmt.Println(key,"-",value)
}
stuMap := make(map[string]map[string]string)
stuMap["学生1"]=make(map[string]string,2)
stuMap["学生1"]["name"] = "李威彤1"
stuMap["学生1"]["sex"] = "男"
stuMap["学生2"]=make(map[string]string,2)
stuMap["学生2"]["name"] = "李威彤2"
stuMap["学生2"]["sex"] = "女"
for key1,value1 := range stuMap{
fmt.Println(key1)
for key2,value2 := range value1{
fmt.Printf("\t%v-%v",key2,value2)
}
fmt.Println()
}
// 内置函数len(map1)
fmt.Println("map的len为:",len(stuMap))
}
map切片
原切片=append(原切片,新切片)
package main
import(
"fmt"
)
func main(){
var mapSlice []map[string]string // 切片嘛,不用多个map嵌套
mapSlice = make([]map[string]string,2)
if mapSlice[0] == nil{
mapSlice[0] = make(map[string]string,2)
mapSlice[0]["name"] = "牛魔王"
mapSlice[0]["age"] = "500"
}
if mapSlice[1] == nil{
mapSlice[1] = make(map[string]string,2)
mapSlice[1]["name"] = "狐狸精"
mapSlice[1]["age"] = "400"
}
fmt.Println(mapSlice)
newMapSlice := map[string]string{
"name":"铁扇公主",
"age":"1",
}
mapSlice = append(mapSlice,newMapSlice) // 通过map切片动态增加map
fmt.Println(mapSlice)
}
map排序(新版默认升序)
package main
import(
"fmt"
"sort"
)
func main(){
map1 := make(map[int]int,10) // 已经默认升序了
map1[10] = 100
map1[1] = 11
map1[3] = 33
map1[8] = 88
fmt.Println(map1)
// 排序
var keys []int // 数组切片
for k,_ := range map1{
keys = append(keys,k)
}
sort.Ints(keys)
fmt.Println(keys)
for _,k := range keys{
fmt.Println(k,"-",map1[k]) // 此步较为关键,通过map的k取出映射value值
}
// 先将k遍历后存入切片数组,对切片数组进行排序,
//再将切片遍历出来的key通过map获取对应的映射value值
}
map的注意事项
- map是引用类型,在函数外会直接修改原map。且map能动态增加扩容
func main(){
map1 := make(map[int]int,2) // 这里虽然是2,但map会自动扩容,不报错。
map1[0] = 0
map1[2] = 22
map1[10] = 10
modify(map1)
fmt.Println(map1)
}
func modify(map1 map[int]int){
map1[10] = 100
}
结构体初入(场景map的value通常为struct)
package main
import(
"fmt"
)
func main(){
StuMap1 := make(map[string]Student)
student1 := Student{"lwt1",22,"男"}
student2 := Student{"lwt2",22,"女"}
student3 := Student{"lwt3",21,"男"}
StuMap1["No1"] = student1
StuMap1["No2"] = student2
StuMap1["No3"] = student3
fmt.Println("StuMap1=>",StuMap1)
// 遍历学生信息
for k,v := range StuMap1{
fmt.Println(k,"-",v)
fmt.Println("v.Name=>",v.Name)
fmt.Println("v.Age=>",v.Age)
fmt.Println("v.Sex=>",v.Sex)
}
}
type Student struct{
Name string
Age int
Sex string
}
map练习
package main
import(
"fmt"
)
func main(){
usersMap := make(map[string]map[string]string,10)
fmt.Println(usersMap)
usersMap["tom"] = make(map[string]string,2)
usersMap["tom"]["pwd"] = "999999"
usersMap["tom"]["nickname"] = "汤姆猫"
modifyUser(usersMap,"lwt")
modifyUser(usersMap,"tom")
fmt.Println(usersMap)
}
/*
传入姓名,若姓名存在于map中,将该用户的密码修改为888,若不存在该姓名,则增添用户
*/
func modifyUser(usersMap map[string]map[string]string,name string){
if usersMap[name] != nil{ // 如果userMap不为空,其必存在pwd密码
usersMap[name]["pwd"] = "888888"
}else{
usersMap[name] = make(map[string]string,2)
usersMap[name]["pwd"] = "888888"
usersMap[name]["nickname"] = "昵称~"+name
}
}