码迷,mamicode.com
首页 > 其他好文 > 详细

go 集合

时间:2018-12-27 03:27:52      阅读:186      评论:0      收藏:0      [点我收藏+]

标签: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)

  

go 集合

标签:range   输出   python   margin   delete   japan   col   var   删除   

原文地址:https://www.cnblogs.com/Jack-cx/p/10182650.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!