Go语言集合(Map)

package main

import "fmt"

func main (){
	var countryCaptiaMap map[string]string
	
	/*创建集合*/
	countryCaptiaMap =make(map[string]string)
	//10
	/*map中插入k-v对 ,各个国家的首都*/
	countryCaptiaMap["France"] = "Paris"
	countryCaptiaMap["Italy"] = "Rome"
	countryCaptiaMap["Japan"] = "tokyo"
	countryCaptiaMap["India"] = "New Delhi"
	
	/*使用key输出map值*/
	for country :=range countryCaptiaMap{
		fmt.Println("Caption == ",country, "it`s ",countryCaptiaMap[country])
	}//20
	
	/*查看元素在集合中是否存在*/
	caption ,ok :=countryCaptiaMap["United States"]
	if(ok){
		fmt.Println("Caption of Unite States is", caption)
	}else {
		fmt.Println("不存在")
	}
	/*打印原始Map*/
	//30
	for country := range countryCaptiaMap {
		fmt.Println("Caption of",country ,"is",countryCaptiaMap[country])
	}
	
	/*删除元素*/
	delete (countryCaptiaMap, "France")
	
	/**打印现在的Map*/
	for country := range countryCaptiaMap {
		fmt.Println("Caption of",country ,"is",countryCaptiaMap[country])
	}
}

你可能感兴趣的:(Go语言集合(Map))