In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, Xiaobian will bring you a comprehensive analysis of how to carry out golang language map. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.
Map1. Basic Introduction
Map is a key-value data structure, also known as a field or associative array. A collection of languages similar to other programming languages that are frequently used in programming.
2. Declare basic syntax
var map Variable name map[keytype]valuetype
What type can key be: map in golang, key can be many types, such as bool, number, string, pointer, channel , or interface, structure, array containing only the previous types
Usually key is int, string Note: slice, map and function are not allowed, because these cannot be judged with ==
What type of valuetype can be: valuetype is basically the same as key, I won't repeat it here Usually: number (integer, floating point number),string,map,struct
3. Examples of declarations
Examples of map declarations:
var a map[string]string
var a map[string]int
var a map[int]string
var a map[string]map[string]string
Note: declaration is not allocated memory, initialization needs to make, allocation of memory before assignment and use
package mainimport ( "fmt") func main(){ //map declaration notes var a map[string]string //Before using map, first make allocated data space. a = make(map[string]string,10) a["no1"] = "Song Jiang" a["no2"] = "Wu Yong" a["no3"] = "Wu Song" a["no1"] = "Wu Yong" fmt.Println(a)}
Notes:
1. Before use, you must make otherwise the compilation will not pass, because there is no space, so you must first apply for space.
2. The above code and results show that the key value in the map cannot be repeated,(if repeated, the last key-value is the main) the value can be repeated
make built-in function
二、map 的使用
1.map声明三种方式
方式1:
var a map[string]string // 在使用map前,首先要make 分配数据空间。 a = make(map[string]string,10) a["no3"] = "松江" a["no1"] = "吴用" a["no3"] = "武松" a["no2"] = "吴用" fmt.Println(a)
方式2:
//第二种方式 cities := make(map[string]string) cities["no1"] = "上海" cities["no2"] = "西安" cities["no3"] = "天津" fmt.Println(cities)
方式3:
//第三种方式 heroes := map[string]string{ "no1" : "chengdu", // 注意不能少了" ,"号 "no2" : "beijing", "no3" : "wuhan", } fmt.Println(heroes)2.map[string]map[string]string使用案例
演示一个 key-value 的 value 是 map 的案例
比如:我们要存放 3 个学生信息, 每个学生有 name 和 sex 信息
思路: map[string]map[string]string
三、map 的增删改查操作1.map 增加和更新
map["key"] = value //如果 key 还没有,就是增加,如果 key 存在就是修改
cities := make(map[string]string) cities["no1"] = "上海" cities["no2"] = "西安" cities["no3"] = "天津" fmt.Println(cities) //因为no3 这个key值已经存在,所以下面的就是修改,若无就是增加 cities["no3"] = "天津..." fmt.Println(cities)2.map 删除
delete(map,"key") ,delete 是一个内置函数,如果 key 存在,就删除该 key-value,如果 key 不存在, 不操作,但是也不会报错
//删除演示 delete(cities,"no1") fmt.Println(cities) //当delete指定的key不存在时,删除不操作,也不会报错 delete(cities,"no4") fmt.Println(cities)
如果我们要删除 map 的所有 key ,没有一个专门的方法一次删除,可以遍历一下 key, 逐个删除 或者 map = make(...),make 一个新的,让原来的成为垃圾,被 gc 回收
注意如果要全部删除,两种方式
遍历所有key,逐一删除直接
make一个新空间。
//如果希望一次性删除所有 两种方式 //1.遍历所有key,逐一删除 //2.直接make一个新空间。 cities := make(map[string]string) fmt.Println(cities)3.map 查找//演示map查找 val , ok :=cities["no2"] if ok{ fmt.Printf("找到了 值为%v",val) }else{ fmt.Printf("没有找到") }
说明:如果 cities 这个 map 中存在 "no2" , 那么 findRes 就会返回 true,否则返回 flase
四、map的其他操作1.map 遍历:
案例演示相对复杂的 map 遍历:该 map 的 value 又是一个 map
说明:map 的遍历使用 for-range 的结构遍历
package mainimport ( "fmt") func main(){ cities := make(map[string]string) cities["no1"] = "上海" cities["no2"] = "西安" cities["no3"] = "天津" for k , v :=range cities{ fmt.Printf("k=%v v=%v \n",k,v) }}
复杂遍历案例
2.map 的长度
package mainimport ( "fmt") func main(){ cities := make(map[string]string) cities["no1"] = "上海" cities["no2"] = "西安" cities["no3"] = "天津" for k , v :=range cities{ fmt.Printf("k=%v v=%v \n",k,v) } fmt.Println(len(cities)) //3}3.map 切片
1.基本介绍
切片的数据类型如果是 map,则我们称为 slice of map,map 切片,这样使用则 map 个数就可以动 态变化了。
2.案例
package mainimport ( "fmt") func main(){ monsters := make([]map[string]string,2) if monsters[0] == nil{ monsters[0] = make(map[string]string,2) monsters[0]["name"] = "牛魔王" monsters[0]["age"] = "500" } if monsters[1] == nil{ monsters[1] = make(map[string]string,2) monsters[1]["name"] = "玉兔精" monsters[1]["age"] = "400" } // 这里如果我们继续使用monsters[2] 肯定越界 所以我们需要动态追加 //这里我们需要使用切片的append函数,可以增加monsters //演示: newMonsters := map[string]string{ "name" : "火云邪神", "age" : "200", } //追加 monsters = append(monsters,newMonsters) fmt.Println(monsters)}
4.map 排序
1.基本介绍
golang 中没有一个专门的方法针对 map 的 key 进行排序
golang 中的 map 默认是无序的,注意也不是按照添加的顺序存放的,你每次遍历,得到的输出 可能不一样.
golang 中 map 的排序,是先将 key 进行排序,然后根据 key 值遍
2.案例演示
map1 := make(map[int]int,100) map1[10] = 100 map1[1] = 13 map1[4] = 56 map1[8] = 90 for k , v :=range map1{ fmt.Printf("k=%v v=%v \n",k , v) //无序的 }
没有排序 下来进行排序
//如果按照map的key的顺序进行排序输出 //1. 先将map的key 放入到切片中 //2.对切片排序 //3. 遍历切片,然后按照key来输出map的值 var keys[]int for k , _ := range map1{ keys = append(keys,k) } //排序 sort.Ints(keys) fmt.Println(keys) //输出key值 for _ , k :=range keys{ fmt.Printf("map1[%v]=%v \n",k,map1[k]) }五、map 使用细节
1) map 是引用类型,遵守引用类型传递的机制,在一个函数接收 map,修改后,会直接修改原来 的 map
package mainimport ( "fmt") func modify(map2 map[int]int ){ map2[10] = 900} func main(){ //map是引用类型,遵守引用类型传递机制,在一个函数接受map //修改后,会直接修改原来的map map1 := make(map[int]int) map1[1] = 90 map1[2] = 88 map1[10] = 1 map1[20] = 2 modify(map1) fmt. Println(map1) }
2)map 的容量达到后,再想 map 增加元素,会自动扩容,并不会发生 panic,也就是说 map 能动 态的增长 键值对(key-value)
3) map 的 value 也经常使用 struct 类型,更适合管理复杂的数据(比前面 value是一个 map 更好)
上述就是小编为大家分享的如何进行golang语言map全方位的分析了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.