Go language basics map supplement
In the previous section, we learned how to use map.
map can be defined in two ways, one is the standard way, assigning a value when declaring, and the other is make.
package main import "fmt" func main() { var stu1 = map[string]string{ "Name": "张三", "Age": "18", "height": "188", //每行都要以,结尾, } stu2:=make(map[string]string,10) stu2["Name"] = "李四" stu2["Age"] = "170" stu2["height"] = "109" fmt.Println(stu1,stu2) //map[Age:18 Name:张三 height:188] map[Age:170 Name:李四 height:109] }
Found a problem
I don’t know if you have found a problem with the map we are currently storing It seems that only one can be stored.
Generally speaking, I should have a list that stores student information one by one.
Pseudocode:
var student_list = [张三的信息,李四的信息,王五的信息,...]
But after playing for so long, there are still single pieces of information. It's bad, it's harmful.
The map is stored in the slice
We know the slice, is defined like this.
var names []string var names = []string{} var names = make([]string,0,10)
In the above, the lists are saved with basic types, strings, numbers, etc.
For something exciting, store the map directly in the list.
伪代码
var names = []map[string]string{} //注意:map[string]string{}是切片里面值的类型,这个切片里面的每个值都是map类型
示例代码
package main import "fmt" func main() { //定义个类型是map的列表 var names = []map[string]string{} var name1 = map[string]string{ "Name": "张三", "Age": "18", } var name2 = map[string]string{ "Name": "李四", "Age": "22", } names = append(names, name1, name2) //向列表中添加map fmt.Println(names) //[map[Age:18 Name:张三] map[Age:22 Name:李四]] }
图解
map中保存切片
在以往的操作中,我们的map的key和value都是一个值。
那像一个人的爱好了,生活习惯了,等,都不止一个,所以,在map中,map的值(value),应该能保存多个才对。
代码
package main import "fmt" func main() { //map中,value为切片类型 var person_hoppy = map[string][]string{} person_hoppy["hoppy"] = []string{"吃", "喝", "拉", "撒"} fmt.Println(person_hoppy)//map[hoppy:[吃 喝 拉 撒]] }
图解
注:在map中,key只能是固定值,因为要通过key找值,所以key不能修改,value可以是任意类型。
The above is the detailed content of Go language basics map supplement. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Go pointer syntax and addressing problems in the use of viper library When programming in Go language, it is crucial to understand the syntax and usage of pointers, especially in...
