Go中的map是无序的,如果想对map进行排序,需要进行以下三步:
1、遍历map,将map的key值存入一个切片
2、对切片进行排序
3、遍历切片,输出对应的map的值
var map1 = make(map[int]int, 10) map1[10] = 100 map1[1] = 13 map1[8] = 90 map1[4] = 56 var keys []int for k, _ := range map1 { keys = append(keys, k) } sort.Ints(keys) for _, k := range keys { fmt.Printf("map1[%v]=%v\n", k, map1[k]) }