Home > Article > Backend Development > An article to help you understand the basics of Go language map
Hey, everyone, I am Zhou Ba. This time we will continue to learn the basics of Go map.
In the above-mentioned multiple articles, we Learned data types,arrays,Slicing etc. help us store data.
Especially slices, which can store multiple things and can be flexibly added, deleted, modified, and searched.
But slicing still has some inconveniences.
For example, a student has multiple information, such as Name,Height,Weight,Agewait.
If using slices, we may store it like this.
package main import "fmt" func main() { //学生1 var stu1 = []string{"张三", "188", "70KG", "18"} //学生2 var stu2 = []string{"李四", "170", "66KG", "17"} fmt.Println(stu1) fmt.Println(stu2) }
According to the current knowledge, we can only go so far, and I can only think of this.
But there will be an obvious disadvantage, that is, we need to use the subscript to get the value or Modify the value .
And we have to count where the subscript of the value we want to modify is, which is relatively not that convenient.
map is called a dictionary in Python and Java It is also called map in PHP. Lists in PHP seem to have the function of map.
map is a key-value pair (key-value) storage structure, which is Unordered, internally implemented using hash, with high performance.
In Go, map is of type reference, and the memory map is as follows.
方式一,声明时赋值 var 变量名 = map[key类型][value类型]{ key:value, key:value,//必须使用,结尾,否则会报错 } //方式二,make方式 var 变量名 = make(map[key类型]value类型, 容量(cap)) //如果map是make方式声明的,第二个参数直接就是容量,元素个数是0,没有第三个参数
代码
package main import "fmt" func main() { var stu1 = map[string]string{ "Name": "张三", "Age": "18", "height": "188", //每行都要以,结尾, } var stu2 = map[string]string{ "Name": "李四", "Age": "20", "height": "170", //每行都要以,结尾, } fmt.Println(stu1, stu2) //map[Age:18 Name:张三 height:188] map[Age:20 Name:李四 height:170] }
代码
package main import "fmt" func main() { var stu1 = make(map[string]string,10) stu1["Name"] = "张三" stu1["Age"] = "18" stu1["height"] = "188" var stu2 = make(map[string]string,10) stu2["Name"] = "李四" stu2["Age"] = "20" stu2["height"] = "170" fmt.Println(stu1,stu2) //map[Age:18 Name:张三 height:188] map[Age:20 Name:李四 height:170] }
ps:关于这两种方式,哪个使用的多。
我的建议是,如果确定有多少个字段,就使用第一种,如果不确定多少个字段,是动态添加的,用第二种。
使用第二种要大概估算好容量,超过会触发自动扩容机制,可能会产生那么一丝丝的性能影响。
遍历map,通常只用一种方式for range
。
代码
package main import "fmt" func main() { var stu1 = make(map[string]string, 10) stu1["Name"] = "张三" stu1["Age"] = "18" stu1["height"] = "188" for key, value := range stu1 { //map遍历时,key值键,value是值 fmt.Println(key, value) } }
只遍历key
package main import "fmt" func main() { var stu1 = make(map[string]string, 10) stu1["Name"] = "张三" stu1["Age"] = "18" stu1["height"] = "188" for key := range stu1 { //只遍历key fmt.Println(key) } }
package main import "fmt" func main() { //var stu1 = make(map[string]string, 10) //stu1["Name"] = "张三" //stu1["Age"] = "18" //stu1["height"] = "188" // //stu1["Name"] = "张三666"//修改 //fmt.Println(stu1) //同上 var stu1 = map[string]string{ "Name": "张三", "Age": "18", "height": "188", //每行都要以,结尾, } stu1["Name"] = "张三666"//修改 fmt.Println(stu1) }
删除map里面的值需要用到delete
。
代码
package main import "fmt" func main() { var stu1 = map[string]string{ "Name": "张三", "Age": "18", "height": "188", //每行都要以,结尾, } fmt.Println(stu1) //map[Age:18 Name:张三 height:188] delete(stu1, "Name") //删除key以及key对应的值 fmt.Println(stu1) //map[Age:18 height:188] }
map在取值时,尽可能的判断一下是否key存在
package main import "fmt" func main() { var stu1 = map[string]string{ "Name": "张三", "Age": "18", "height": "188", //每行都要以,结尾, } //result := stu1["Name"]//key存在,没问题 //fmt.Println(result)//张三 //result := stu1["Names"]//手一抖,key打错了 //fmt.Println(result)//结果为空,显然不是太友好 //取值标准用法 result, ok := stu1["Name"] //如果key存在,ok为true,如果key不存在,ok为false fmt.Println(result,ok)//张三 true if ok { fmt.Println(result) } else { fmt.Println("key不存在") } }
上述我们学习了Go基础之map。如果在操作过程中有任何问题,记得下面讨论区留言,我们看到会第一时间解决问题。
The above is the detailed content of An article to help you understand the basics of Go language map. For more information, please follow other related articles on the PHP Chinese website!