Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Map on the basis of Go language

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains how to use Map on the basis of the Go language. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use Map on the basis of the Go language.

Introduction of map

Map, called dictionary in Python, also called map,PHP in Java, seems to have the function of map in list.

Map is a key-value pair (key-value) storage structure, which is unordered and internally implemented using hash (hash) with high performance.

In Go, map is a reference type

Basic use of map

Map syntax

Method 1, assign var variable name = map [key type] [value type] {key:value, key:value,// must be used, ending, otherwise an error will be reported} / / method 2. Make mode var variable name = make (map [key type] value type, capacity (cap)) / / if map is declared in make mode, the second parameter is directly capacity, the number of elements is 0, and there is no third parameter.

Example

Method 1, assign a value when declaring

Code

Package main import "fmt" func main () {var stu1 = map [string] string {"Name": "Zhang San", "Age": "18", "height": "188", / / each line ends with,} var stu2 = map [string] string {" Name ":" Li Si "," Age ":" 20 " "height": "170", / / each line should end with,} fmt.Println (stu1, stu2) / / map [Age:18 Name: Zhang San height:188] map [Age:20 Name: Li Si height:170]}

Mode 2, make mode

Code

Package main import "fmt" func main () {var stu1 = make (map [string] string,10) stu1 ["Name"] = "Zhang San" stu1 ["Age"] = "18" stu1 ["height"] = "188" var stu2 = make (map [string] string,10) stu2 ["Name"] = "Li Si" stu2 ["Age"] = "20" stu2 ["height"] = "170" fmt.Println (stu1 Stu2) / / map [Age:18 Name: Zhang San height:188] map [Age:20 Name: Li Si height:170]}

Ps: which of these two methods is used more often?

My advice is to use the first if you are sure how many fields there are, and if you are not sure how many fields are dynamically added, use the second.

Using the second method, it is necessary to roughly estimate the capacity, which will trigger the automatic expansion mechanism, which may have the slightest performance impact.

Traversing map (check)

Traversing map, usually for range in only one way.

Code

Package main import "fmt" func main () {var stu1 = make (map [string] string, 10) stu1 ["Name"] = "Zhang San" stu1 ["Age"] = "18" stu1 ["height"] = "188" for key, value: = range stu1 {/ / map traversal, key key, value is the value fmt.Println (key, value)}}

Traversing only key

Package main import "fmt" func main () {var stu1 = make (map [string] string, 10) stu1 ["Name"] = "Zhang San" stu1 ["Age"] = "18" stu1 ["height"] = "188" for key: = range stu1 {/ / only traverses key fmt.Println (key)}}

Modify the value of map (change)

Package main import "fmt" func main () {/ / var stu1 = make (map [string] string 10) / stu1 ["Name"] = "Zhang San" / / stu1 ["Age"] = "18" / / stu1 ["height"] = "188" / stu1 ["Name"] = "Zhang San 666" / / modify / / fmt.Println (stu1) / / ditto var stu1 = map [string] string {"Name": "Zhang San" "Age": "18", "height": "188", / / each line should end with,} stu1 ["Name"] = "Zhang San 666" / / modify fmt.Println (stu1)}

Delete the value in map

You need to use delete to delete the value in map.

Code

Package main import "fmt" func main () {var stu1 = map [string] string {"Name": "Zhang San", "Age": "18", "height": "188", / / each line ends with,} fmt.Println (stu1) / / map [Age:18 Name: Zhang San height:188] delete (stu1 "Name") / / Delete key and the value fmt.Println (stu1) / / map [Age:18 height:188]} corresponding to key

Considerations for the value of map

When taking a value of map, try your best to determine whether key exists.

Package main import "fmt" func main () {var stu1 = map [string] string {"Name": "Zhang San", "Age": "18", "height": "188", / / each line ends with,} / / result: = stu1 [" Name "] / / key exists No problem / / fmt.Println (result) / / Zhang San / / result: = stu1 ["Names"] / / hand shake, key typed wrong / / fmt.Println (result) / / the result is empty, obviously not very friendly / / value standard usage result, ok: = stu1 ["Name"] / / if key exists, ok is true, if key does not exist, ok is false fmt.Println (result) Ok) / / Zhang San true if ok {fmt.Println (result)} else {fmt.Println ("key does not exist")} thank you for your reading. The above is the content of "how to use Map on the basis of Go language". After the study of this article, I believe you have a deeper understanding of how to use Map on the basis of Go language, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report