package main
import (
"fmt"
"sort"
)
// 按map的key排序
func main() {
params := make(map[string]interface{})
params["age"] = 23
params["name"] = "xxx"
params["sex"] = 0
params["level"] = 1
// 1.去除所有的key
var keys []string
for k := range params{
keys = append(keys, k)
}
// 对字符串切片排序
sort.Strings(keys)
fmt.Println(keys)
// 打印key, val
for _, k := range keys{
fmt.Printf("key: %v val:%v \n", k, params[k])
}
}
有疑问加站长微信联系(非本文作者)