标签:range 输出 python margin delete japan col var 删除
1.定义一个集合 感觉和python的字典差不多
func main() { //定义一个集合 var countryCapitalMap map[string]string /*创建集合 */ //创建集合 countryCapitalMap = make(map[string]string) /* map插入key - value对,各个国家对应的首都 */ countryCapitalMap["France"] = "Pais" countryCapitalMap["Italy"] = "罗马" countryCapitalMap["Japan"] = "东京" countryCapitalMap["India "] = "新德里" fmt.Println(countryCapitalMap) }
2.循环输出key value
func main() { var countryCapitalMap map[string]string /*?????oé????? */ countryCapitalMap = make(map[string]string) /* map??’?…¥key - value?ˉ1,?????a???????ˉ1?o”???é|–é?? */ countryCapitalMap["France"] = "Paris" countryCapitalMap["Italy"] = "??—é??" countryCapitalMap["Japan"] = "????o?" countryCapitalMap["India "] = "?–°??·é??" for country := range countryCapitalMap { fmt.Println(country, countryCapitalMap[country]) } }
3.判断key是否存在
var countryCapitalMap map[string]string /*创建集合 */ countryCapitalMap = make(map[string]string) /* map插入key - value对,各个国家对应的首都 */ countryCapitalMap["France"] = "Paris" countryCapitalMap["Italy"] = "罗马" countryCapitalMap["Japan"] = "东京" countryCapitalMap["India "] = "新德里" //ok为bool值,为true那么captial就是这个key的value captial, ok := countryCapitalMap["Italy"] if ok { fmt.Println(ok, "存在首都是", captial) } else { fmt.Println("不存在") }
4.删除key value
var countryCapitalMap map[string]string /*?????oé????? */ countryCapitalMap = make(map[string]string) /* map??’?…¥key - value?ˉ1,?????a???????ˉ1?o”???é|–é?? */ countryCapitalMap["France"] = "Paris" countryCapitalMap["Italy"] = "??—é??" countryCapitalMap["Japan"] = "????o?" countryCapitalMap["India "] = "?–°??·é??" delete(countryCapitalMap, "France") fmt.Println(countryCapitalMap)
标签:range 输出 python margin delete japan col var 删除
原文地址:https://www.cnblogs.com/Jack-cx/p/10182650.html