go map和切片结合使用,map 排序,删除
map和切片结合使用
package main
import(
"fmt"
"math/rand"
"time"
)
func testSliceMap(){
rand.Seed(time.Now().UnixNano())
var s []map[string]int
s = make([]map[string]int, 5, 16)
for index, val:=range s{
fmt.Printf("----------slice[%d]=%v\n",index,val)
}
// s[0]["stu01"] = 10 // panic: assignment to entry in nil map 必须先对切片中的元素初始化才行
fmt.Println()
s[0] = make(map[string]int,16)
s[0]["stu001"] =1
s[0]["stu002"] =2
s[0]["stu003"] =3
s[0]["stu004"] =4
for index,val:=range s{
fmt.Printf("slice[%d]=%v\n",index,val)
}
}
func testMapSlice1(){
rand.Seed(time.Now().UnixNano())
var s map[string][]int
s = make(map[string][]int,16)
key := "stu01"
value,ok := s[key]
if !ok{
fmt.Println("你好001",s,value,ok)
s[key]= make([]int,0,16)
value = s[key]
}
fmt.Println("你好002",s,value,ok)
value = append(value,100)
fmt.Println("你好003",value)
value = append(value,200)
fmt.Println("你好004",value)
value = append(value,300)
fmt.Println("你好005",value)
s[key] = value
fmt.Printf("map:%v\n",s)
}
func main(){
testSliceMap()
testMapSlice1()
}
输出:
----------slice[0]=map[]
----------slice[1]=map[]
----------slice[2]=map[]
----------slice[3]=map[]
----------slice[4]=map[]
slice[0]=map[stu001:1 stu002:2 stu003:3 stu004:4]
slice[1]=map[]
slice[2]=map[]
slice[3]=map[]
slice[4]=map[]
你好001 map[] [] false
你好002 map[stu01:[]] [] false
你好003 [100]
你好004 [100 200]
你好005 [100 200 300]
map:map[stu01:[100 200 300]]
删除,排序
package main
import (
"fmt"
"math/rand"
"sort"
"time"
)
func testMapSort(){
rand.Seed(time.Now().UnixNano())
var a map[string]int = make(map[string]int,1024)
for i:=0;i<5;i++{
key:=fmt.Sprintf("stu%d",i)
value := rand.Intn(100)
a[key] =value
}
fmt.Printf("001 a=%#v\n",a)
delete(a,"stu3")
fmt.Printf("002 a=%#v\n",a)
var keys []string = make([]string, 0, 12)
for key,value :=range a{
fmt.Printf("map[%s]=%d\n", key, value)
keys = append(keys, key)
}
fmt.Printf("keys01 keys:%v\n",keys)
sort.Strings(keys)
fmt.Printf("keys02 keys:%v\n",keys)
for _,value :=range keys{
fmt.Printf("key:%s val:%d\n",value,a[value])
}
}
func main(){
testMapSort()
}
输出:
001 a=map[string]int{"stu0":82, "stu1":67, "stu2":32, "stu3":76, "stu4":2}
002 a=map[string]int{"stu0":82, "stu1":67, "stu2":32, "stu4":2}
map[stu4]=2
map[stu2]=32
map[stu0]=82
map[stu1]=67
keys01 keys:[stu4 stu2 stu0 stu1]
keys02 keys:[stu0 stu1 stu2 stu4]
key:stu0 val:82
key:stu1 val:67
key:stu2 val:32
key:stu4 val:2
map 未初始化 nil
package main
import(
"fmt"
)
func testMap(){
var a map[string]int
fmt.Printf("001 a:%#v\n",a)
// a["stu01"] =100 // 必须先初始化才能赋值否则报错 panic: assignment to entry in nil map
if a ==nil{
a = make(map[string]int ,16) // map的容量要提前预估下,否则存储不下时会扩容,造成性能消耗
fmt.Printf("002 a:%#v\n",a)
if a == nil{
fmt.Printf("003 实例化了 a:%#v\n",a)
}else {
fmt.Printf("004 实例化了 a:%#v\n",a)
}
a["stu01"] = 10
fmt.Printf("a=%#v\n",a)
a["stu01"] = 11
a["stu02"] = 12
a["stu03"] = 13
a["stu04"] = 14
a["stu05"] = 15
fmt.Printf("a=%#v\n",a)
}
}
// map申明的时候初始化
func testMapInit(){
var a map[string]int = map[string]int{
"stu01":11, "stu02":12, "stu03":13, "stu04":14, "stu05":15,
}
fmt.Printf("a:%#v\n",a)
a["stu01"] = 101
a["stu02"] = 102
fmt.Printf("a=%#v\n",a)
var key string ="stu04"
fmt.Printf("the value of key[%s] is : %d\n",key,a[key])
}
func main(){
testMap()
testMapInit()
}
输出:
001 a:map[string]int(nil)
002 a:map[string]int{}
004 实例化了 a:map[string]int{}
a=map[string]int{"stu01":10}
a=map[string]int{"stu01":11, "stu02":12, "stu03":13, "stu04":14, "stu05":15}
a:map[string]int{"stu01":11, "stu02":12, "stu03":13, "stu04":14, "stu05":15}
a=map[string]int{"stu01":101, "stu02":102, "stu03":13, "stu04":14, "stu05":15}
the value of key[stu04] is : 14
写入自己的博客中才能记得长久