Go Basic-map

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. //XUHH_GO_MAP Project
/*
Features of 1.map:
A.map is a built-in reference type for the go language, so when multiple maps point to the same underlying situation, a value changes and all changes
B.map are unordered, and the order of each iteration is indeterminate.
C.map only Len has no cap.
D.map is not thread-safe and is used in multiple go-routine to lock.
E.map[key]value
Key must support operations for operators (= =,! =), such as Number/string/pointer/array/struct/interface
Value can be of any type.
Creation of 2.map:
A. New to create
MapA: = new (map[int]string)
*mapa = map[int]string{}
(*mapa) [112] = "112"
(*mapa) [110] = "110"
B. Direct declaration creation
var map1 map[int]string = map[int]string{}
MAP2: = map[int]string{
11: "11",
12: "12",
13: "13",
}
C.make to create
MAP3: = Make (map[int]string, 1000)//1000 is the capacity of the map, this is the same as slice, the pre-application of a piece of memory, to avoid frequent expansion later, improve performance
Some basic operations of 3.MAP
A. Determine if the key value exists
If V, OK: = Map1[5]; OK = = true {
Fmt. PRINTLN ("This key is Exit", V)
} else {
Fmt. PRINTLN ("This key isn't Exit")
}
B. Increase
Map[key] = value
C. Modifications
Map when modifying value
If value is a value type, you need to replace it all if you want to change it (value for value)
If it is a reference type, you can directly assign a value to replace
MapS: = map[string]person{}
maps["xuhh"] = Person{name: "Xuhh", old:26}
maps["xuhh"].name = "Xuhehe"//Error  cannot assign to maps["Xuhh"].name
D. Delete
Key-value pairs can be safely removed during iteration
Delete (map, key)
E. Iterative traversal
For k, V: = Range Map {
Fmt. Println ("key =", K, "value =", V)
}
*/
Package Mainimport ("FMT") the type person struct {name string old Int}func main () {//new is created MapA: = new (M        ap[int]string) *mapa = map[int]string{} (*mapa) [*mapa] = "the" [[]] = "A" for V: = Range *mapa { Fmt. Println (v)}//directly created, can be initialized directly: var map1 map[int]string = map[int]string{} map2: = map[int]string{11: "11 ",": "", "" "," "," FMT. " Println (MAP2) map1[2] = "222" map1[3] = "333" map1[1] = "111" map1[6] = "666" map1[8] = "888" map1[5] = "555" FMT. PRINTLN (MAP1)//Make create MAP3: = Made (map[int]string, +) map3[22] = "$" FMT. PRINTLN (MAP3)//Map operation//Find if V, OK: = Map1[5]; OK = = true {fmt. PRINTLN ("This key is Exit", V)} else {fmt. PRINTLN ("This key was not exit")}//add map1[5] = "Exit" FMT. Println (map1[5])//modify MapS: = map[string]person{} maps["xuhh"] = Person{name: "Xuhh", old:26}//maps["Xuh H "].name =" XuheHe "//error cannot assign to maps[" Xuhh "].name//map when operating value, if value is a value type, to change the value, you need to replace all (value for value), if not a reference type , you can directly assign a value to replace//delete Delete (MAP1, 5) If _, OK: = Map1[5];!ok {fmt. PRINTLN ("This key is deleted")}//Iterate over for k, V: = Range Map1 {fmt. Println ("key =", K, "value =", V)}}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.