是的map
,或者更确切地说map[string]int
默认情况下,据我了解,Golang
它按字母顺序按键对元素进行排序。如何map
按给定顺序按键排序?
这段代码:
package main
import (
"fmt"
)
func main() {
output := make(map[string]int)
basket := map[string]int{
"orange": 5,
"apple": 7,
"mango": 3,
"strawberry": 9,
}
//нужный порядок
keys := []string{
"strawberry",
"mango",
"orange",
}
for _, k := range keys {
output[k] = basket[k]
}
fmt.Printf("вывод output : %+v \n", output)
}
将生成带有按字母顺序排列的键的行:
mango, orange, strawberry