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 in GE language

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

Share

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

This article mainly explains "how to use map in GE language". The content in the article 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 in Goe language".

Syntax declaration and initialization

Use with make, otherwise it is nil

Var map [KeyType] ValueType//KeyType: indicates the type of the key / / ValueType: the type make (map [KeyType] ValueType, [cap]) / / cap represents the capacity of the map. Although this parameter is not required, we should specify an appropriate capacity for the map when initializing it / / automatically expand when your capacity exceeds

Example:

/ / 1. Declare that var name1 map [string// key] is int,value is stringvar name2 map [string] string// key is string// literal quantity names3:= map [int] string {0: "Zhang San", 1: "Li Si"} / / 2. Allocate the capacity of memory name1 = make (map [string], 2) / / 2:map. When your capacity exceeds, it will automatically expand name2 = make (map [string] string, 2) / / 3. Assignment / / 3.1 through KeyType assignment name1 [0] = "Zhang San" / / key:0 value: Zhang San name1 [1] = "Li Si" / / key:1 value: Li Si name1 [2] = "Wang Wu" / / key:2 value: Wang Wu / / has exceeded the allocated capacity and will automatically expand name2 ["A"] = "Zhang San" name2 ["B"] = "Li Si" to read.

Read according to KeyType

Returns that value if keyType exists, and the type default value if it does not exist

Fmt.Println (name1 [0]) fmt.Println (name2 ["B"])

Test whether the key value exists in map

When fetching a value through KeyType, two values are returned, one is vlue, and the other is whether the bool exists.

If value, ok: = name1 [5]; ok {fmt.Println ("this value exists", value)} else {fmt.Println ("this value does not exist", value)} delete delete (map [int] string, [KeyType])

Examples

Delete (name1, 1) traverses range

Example:

For key,value:=range name1 {fmt.Printf ("key=%d,value=%s\ n", key,value)} for key,value:=range name2 {fmt.Printf ("key=%s,value=%s\ n", key,value)} Summary

Map must allocate memory space with make, otherwise it is nil

Literal initialization required to ensure that key is unique, otherwise error

Map passes a reference, which means that the parameter changes the value of the argument

Objects obtained by using the [] operator directly on map objects cannot modify the state directly.

Pointers are stored in map instead of structures

Example package mainimport "fmt" func main () {/ / 1, declare and initialize [for use with make, otherwise it is nil] / / 1. Declare that var name1 map [int] string / / key is int,value is string var name2 map [string] string / / key is string,value is string / / 2. Allocate the capacity of memory name1 = make (map [string], 2) / / 2:map. When your capacity exceeds, it will automatically expand name2 = make (map [string] string, 2) / / 3. Assignment / / 3.1 through KeyType name1 [0] = "Zhang San" / / key:0 value: Zhang San name1 [1] = "Li Si" / / key:1 value: Li Si name1 [2] = "Wang Wu" / / key:2 value: Wang Wu / / has exceeded the allocated capacity Will automatically expand name2 ["A"] = "Zhang San" name2 ["B"] = "Li Si" / / 2, read / / 1. Read according to KeyType, if keyType exists, return that value, if not, return type default value fmt.Println (name1 [0]) fmt.Println (name2 ["B"]) / / 2. Test whether the key value exists in map [when fetching the value through KeyType, two values are returned, one is vlue, the other is whether the bool exists] if value, ok: = name1 [2] Ok {fmt.Println ("this value exists", value)} else {fmt.Println ("this value does not exist", value)} / / 3. Delete delete (name1, 1) / / delete Li Si delete (name2) "C") / / without this C fmt.Println (name1) fmt.Println (name2) / / IV. Traversing for key,value:=range name1 {fmt.Printf ("key=%d,value=%s\ n", key,value)} for key,value:=range name2 {fmt.Printf ("key=%s,value=%s\ n", key) Value)}} Thank you for your reading The above is the content of "how to use map in GE language". After the study of this article, I believe you have a deeper understanding of how to use map in Goe 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