4.3.3.修改映射 元素,也就是“键-值”对,可以被插入到映射中,也可以从映射中删除掉,且任何键对应的值都可以被修改。请看下面的例子。
| fmt.Println(len(populationForCity), populationForCity) delete(populationForCity, "Shanghai") // Delete fmt.Println(len(populationForCity), populationForCity) populationForCity["Karachi"] = 11620000 // Update fmt.Println(len(populationForCity), populationForCity) populationForCity["Beijing"] = 11290000 // Insert fmt.Println(len(populationForCity), populationForCity) 4 map[Shanghai:13680000 Mumbai:12690000 Istanbul:12610000 Karachi:10620000] 3 map[Mumbai:12690000 Istanbul:12610000 Karachi:10620000] 3 map[Mumbai:12690000 Istanbul:12610000 Karachi:11620000] 4 map[Mumbai:12690000 Istanbul:12610000 Karachi:11620000 Beijing:11290000] |
映射的插入和更新语法是一样:如果给定的键不存在,则将给定的键和值插入到该映射中;而如果存在,则将…