map的两种定义方式
package main
import "fmt"
func main() {
//map的定义
//key-value key一般为int/string
//key不能重复,会覆盖, key是无序的,map是完全是无序的。
//第一种方式 使用make
map1 := make(map[string]string, 10)
map1["a"]="hello"
fmt.Println(map1)
//第二种方式 声明直接赋值
map2 := map[string]string{
"a": "hello",
"b": "world",
}
fmt.Println("map2:",map2)
}
定义一个value是map类型的Map,来将学生数据信息存储。
studentmap :=make(map[string]map[string]string,10)
studentmap["1"] = map[string]string{
"name": "tom",
"sex": "male",
"address": "Beijing",
}
studentmap["2"] = map[string]string{
"name": "tom",
"sex": "male",
"address": "Beijing",
}
studentmap["3"] = map[string]string{
"name": "tom",
"sex": "male",
"address": "Beijing",
}
studentmap["4"] = make(map[string]string,3)
studentmap["4"]["name"]= "tom"
studentmap["4"]["sex"]= "male"
studentmap["4"]["address"]= "Tsingdao"
fmt.Println(studentmap)
package main
import (
"fmt"
)
func main() {
//map的定义
//key-value key一般为int/string
//key不能重复,会覆盖, key是无序的,map是完全是无序的。
//第一种方式 使用make
map1 := make(map[string]string, 10)
map1["a"]="hello"
fmt.Println(map1)
//第二种方式 声明直接赋值
map2 := map[string]string{
"a": "hello",
"b": "world",
}
fmt.Println("map2:",map2)
studentmap :=make(map[string]map[string]string,10)
studentmap["1"] = map[string]string{
"name": "tom",
"sex": "male",
"address": "Beijing",
}
studentmap["2"] = map[string]string{
"name": "tom",
"sex": "male",
"address": "Beijing",
}
studentmap["3"] = map[string]string{
"name": "tom",
"sex": "male",
"address": "Beijing",
}
studentmap["4"] = make(map[string]string,3)
studentmap["4"]["name"]= "tom"
studentmap["4"]["sex"]= "male"
studentmap["4"]["address"]= "Tsingdao"
fmt.Println(studentmap)
delete(studentmap,"1")
fmt.Println("删除1后:", studentmap)
//studentmap = make(map[string]map[string]string,10)
//fmt.Println("删除后:", studentmap)
val, ok := studentmap["1"]
if ok{
fmt.Println("有1",val)
}
//遍历map for -range
for k, v := range studentmap{
fmt.Println(k)
for k2,v2 :=range v {
fmt.Println(k2,v2)
}
}
fmt.Println("studentmap有:", len(studentmap))
//map 切片 slice of map map可以动态增加
monsters := make([]map[string]string,2)
monsters[0] =make(map[string]string,2)
monsters[0]["name"] ="fat"
monsters[0]["age"] = "3"
monsters[1] =make(map[string]string,2)
monsters[1]["name"] ="cat"
monsters[1]["age"] = "4"
//我们需要使用append函数,动态增加
fmt.Println("monsters:",monsters)
newmonster := map[string]string{
"name":"love",
"age" : "3",
}
monsters = append(monsters,newmonster)
fmt.Println("增加后的monster:", monsters)
}
对map的keys进行排序
首先创建一个空切片,使用for range将Map的keys全部导入到切片中,使用sort函数对keys切片进行排序,最后使用for range 对map进行输出操作。
//map排序说明
map3 := make(map[int]int,10)
map3[10]=100
map3[1]= 1
map3[4] = 45
//把Key放到切片中
keys := make([]int,0) //定义一个空切片
for k,_:= range map3{
keys= append(keys,k)
}
sort.Ints(keys)
for _,k :=range keys{
fmt.Println(map3[k])
}
使用结构体作为map的value,存储学生信息。
package main
import "fmt"
func main() {
//map的定义
//map的key为学号,map的值为结构体,包含名字,年龄和地址。
type student struct {
Name string
Age int
address string
}
students := make(map[string]student)
stu1 := student{Name:"tom", Age : 18, address: "Beijing"}
stu2 := student{Name:"Jack", Age : 22, address: "Weihai"}
students["1"] = stu1
students["2"] = stu2
fmt.Println(students)
for k,v :=range students{
fmt.Println("编号:", k)
fmt.Println("名字为", v.Name)
fmt.Println("年龄为", v.Age)
fmt.Println("地址为", v.address)
}
}