An article to help you understand the basics of Go language map

Release: 2023-07-25 17:05:16
forward
579 people have browsed it

Preface

Hey, everyone, I am Zhou Ba. This time we will continue to learn the basics of Go map.


Reminiscing about the past

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)
}
Copy after login

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.


Introducing map

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.

An article to help you understand the basics of Go language map

map基本使用

map语法

方式一,声明时赋值
var 变量名 = map[key类型][value类型]{
    key:value,
    key:value,//必须使用,结尾,否则会报错
}
//方式二,make方式
var 变量名 = make(map[key类型]value类型, 容量(cap))
//如果map是make方式声明的,第二个参数直接就是容量,元素个数是0,没有第三个参数
Copy after login

示例

方式一,声明时赋值

代码

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]
}
Copy after login

方式二,make方式

代码

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]
}
Copy after login

ps:关于这两种方式,哪个使用的多。

我的建议是,如果确定有多少个字段,就使用第一种,如果不确定多少个字段,是动态添加的,用第二种。

使用第二种要大概估算好容量,超过会触发自动扩容机制,可能会产生那么一丝丝的性能影响。


遍历map(查)

遍历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)
  }
}
Copy after login

只遍历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)
  }
}
Copy after login

修改map的值(改)

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)
}
Copy after login

删除map里面的值

删除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]
}
Copy after login

map取值注意事项

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不存在")
  }
}
Copy after login

总结

上述我们学习了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!

Related labels:
source:Go语言进阶学习
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!