go 集合
Posted jack-cx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了go 集合相关的知识,希望对你有一定的参考价值。
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)
以上是关于go 集合的主要内容,如果未能解决你的问题,请参考以下文章