4.3.5.映射反转 如果一个映射的值是唯一的且值的类型和键一样都是被支持类型的话,我们就可以很容易的将该映射进行反转操作。
1 2 3 4 5 6 7 | cityForPopulation := make(map[int]string, len(populationForCity)) for city, population := range populationForCity { cityForPopulation[population] = city } fmt.Println(cityForPopulation) map[12610000:Istanbul 11290000:Beijing 12690000:Mumbai 11620000:Karachi] |
因为populationForCity是map[string]int类型的,所以我们开始创建了一个反转的映射…