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 /*创建集合 */
    countryCapitalMap = make(map[string]string)

    /* map插入key - value对,各个国家对应的首都 */
    countryCapitalMap["France"] = "Paris"
    countryCapitalMap["Italy"] = "罗马"
    countryCapitalMap["Japan"] = "东京"
    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 /*创建集合 */
    countryCapitalMap = make(map[string]string)

    /* map插入key - value对,各个国家对应的首都 */
    countryCapitalMap["France"] = "Paris"
    countryCapitalMap["Italy"] = "罗马"
    countryCapitalMap["Japan"] = "东京"
    countryCapitalMap["India "] = "新德里"
    delete(countryCapitalMap, "France")
    fmt.Println(countryCapitalMap)

  

posted @ 2018-12-26 23:43  t_zcx  阅读(619)  评论(0编辑  收藏  举报