12345678910111213 | //1、声明map的方式1var map1 map[string]string//2、声明map的方式2map2 := make(map[string]string)//3、map中key可以是:int、float、bool、string、数组// 一定不可以是:切片、函数、mapvar m1 map[int]stringvar m2 map[float64]stringvar m3 map[bool]stringvar m4 map[string]string |
1、声明时同时初始化
123456789101112 | var country = map[string]string{ "China": "Beijing", "Japan": "Tokyo", "India": "New Delhi", "France": "Paris", "Italy": "Rome",}fmt.Println(country)//短变量声明初始化方式rating := map[string]float64{"c": 5, "Go": 4.5, "Python": 4.5, "C++": 3}fmt.Println(rating) |
2、创建map后再赋值
123456 | countryMap := make(map[string]string)countryMap["China"] = "Beijing"countryMap["Japan"] = "Tokyo"countryMap["India"] = "New Delhi"countryMap["France"] = "Paris"countryMap["Italy"] = "Rome" |
123 | for k, v := range countryMap { fmt.Println("国家", k, "首都", v)} |
123 | for _, v := range countryMap { fmt.Println("国家", "首都", v)} |
123 | for k := range countryMap { fmt.Println("国家", k , "首都", countryMap[k])} |
12345678910111213141516 | value , ok := countryMap["England"]fmt.Printf("%q \n" , value)fmt.Printf("%T , %v \n" , ok , ok)if ok { fmt.Println("首都:" , value)} else { fmt.Println("首都信息未检索到!")}//第二种方式if value ,ok :=countryMap["USA"];ok { fmt.Println("首都:" , value)} else { fmt.Println("首都信息未检索到!")} |
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 | package mainimport "fmt"func main() {//1、声明时同时初始化var country = map[string]string{"China": "Beijing","Japan": "Tokyo","India": "New Delhi","France": "Paris","Italy": "Rome", } fmt.Println(country)//短变量声明初始化方式 rating := map[string]float64{"c": 5, "Go": 4.5, "Python": 4.5, "C++": 3} fmt.Println(rating)//2、创建map后再赋值 countryMap := make(map[string]string) countryMap["China"] = "Beijing" countryMap["Japan"] = "Tokyo" countryMap["India"] = "New Delhi" countryMap["France"] = "Paris" countryMap["Italy"] = "Rome"// 3、遍历map(无序)// (1)、key 、value都遍历for k, v := range countryMap { fmt.Println("国家", k, "首都", v) } fmt.Println("-----------")//(2)、只展示valuefor _, v := range countryMap { fmt.Println("国家", "首都", v) } fmt.Println("-----------")//(3)、只展示keyfor k := range countryMap { fmt.Println("国家", k , "首都", countryMap[k]) } fmt.Println("-----------")//4、查看元素是否在map中存在 value , ok := countryMap["England"] fmt.Printf("%q \n" , value) fmt.Printf("%T , %v \n" , ok , ok)if ok { fmt.Println("首都:" , value) } else { fmt.Println("首都信息未检索到!") }//或者if value ,ok :=countryMap["USA"];ok { fmt.Println("首都:" , value) } else { fmt.Println("首都信息未检索到!") }} |
1234567891011121314151617181920212223242526 | package mainimport "fmt"func main() {//1、声明并初始化一个map map1 := map[string]string {"element":"div","width" :"100px","height":"200px","border":"solid","background":"none", }//2、根据key删除map中的某个元素 fmt.Println("删除前:",map1)if _,ok := map1["background"]; ok {delete(map1 , "background") } fmt.Println("删除后:",map1)//3、清空map//map1 = map[string]string{} map1 = make(map[string]string) fmt.Println("清空后:",map1)} |
本文链接: https://dreamerjonson.com/2018/11/26/golang-17-mapping/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。