Map映射
package main
import "fmt"
func main() {
// 声明Map
var m1 map[int]string
// 初始化空间
m1 = make(map[int]string, 3)
m1[1] = "one"
m1[2] = "two"
m1[3] = "three"
fmt.Println(m1)
fmt.Println(m1[1])
}
package main
import (
"fmt"
"math/rand"
"sort"
"time"
)
func main() {
// 初始化随机数种子
rand.Seed(time.Now().UnixNano())
var scoreMap = make(map[string]int, 200)
for i := 0; i < 100; i++ {
key := fmt.Sprintf("stu%02d", i)
value := rand.Intn(100)
scoreMap[key] = value
}
var keys = make([]string, 0, 200)
for key := range scoreMap {
keys = append(keys, key)
}
// 对切片排序
sort.Strings(keys)
for _, key := range keys {
fmt.Println(key, scoreMap[key])
}
}