The 5th Lesson Go map

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
 PackageMainImport("FMT")/ * Map-similar to a hash table or dictionary in other languages, store data in key-value form-key must be support = = or! = The type of the comparison operation, cannot be a function, map or Slice-map lookup is faster than a linear search ihenduo, but 100 times times slower than the type of data accessed using the index-MAP use make () to create, support: = This shorthand method-make ([Keytype]valuet YPE,CAP), the CAP represents capacity, can be omitted-the capacity is automatically expanded when exceeded, but as far as possible to provide a reasonable initial value-use Len () to get the number of elements-key value pairs do not exist automatically added, use delete () delete a key value pair-use for range to map and s Lice iterative Operation * /funcMain () {varMMap[int]stringm =Map[int]string{} fmt. Println (M)//completed the initialization of a map    varNMap[int]stringn = Make(Map[int]string) FMT. PRINTLN (N)//You can also use make to createP: = Make(Map[int]string) FMT. PRINTLN (P)//Yes, it is.Fmt. Println ("-----") m[1] ="OK"A: = M[2] FMT. Println (a)//Output emptyFmt. Println (M[1])Delete(M,1)//DeleteFmt. Println (M[1])//Delete output empty after    //operation of simple mapFmt. Println ("-----")}
 PackageMainImport("FMT")funcMain () {varMMap[int]Map[int]string     //Create a two layer map herem = Make(Map[int]Map[int]string)//Only one layer of map is initialized hereA, OK: = M[2][1]///Only one return value is returned when the value is taken, the second return value returns whether the key value pair exists    if!ok {//If OK does not exist, initialize the second layer of mapM[2] = Make(Map[int]string)} m[2][1] ="Good"A, OK = m[2][1] FMT. Println (A, OK)//Complex map Operations}

Slice under the For-range

package mainimport (    "fmt")func main() {    forrange slice {        //返回slice相对应的索引,i相当于计数器,v相当于slice所存储的值,是拷贝,不会影响slice本身        //可以通过slice[i]对slice本身进行操作    }}

For-range under the map

package mainimport (    "fmt")func main() {    forrangemap {        //返回k,v键值对(同样是拷贝)        //当然可以对m[k]直接对map进行操作    }}

For-range for map initialization

 PackageMainImport("FMT")funcMain () {sm: = Make([]Map[int]string,5)//This is a map-element slice that initializes the capacity, element 5     for_, V: =RangeSM {v = Make(Map[int]string,1) v[1] ="OK"Fmt. Println (v)} FMT. PRINTLN (SM)/ * Output result Map[1:ok] Map[1:ok] map[1:ok] Map[1:ok] Map[1:ok] [map[] map[ ] map[] map[] map[] "It is clear that V is a copy and cannot operate on the map itself. If you want to operate on itself, you need to make the following modifications (using i) */SMM: = Make([]Map[int]string,5) forI: =RangeSM {Smm[i] = Make(Map[int]string,1) Smm[i][1] ="OK"Fmt. Println (Smm[i])} FMT. PRINTLN (SMM)}

Indirect ordering of maps

 PackageMainImport("FMT"    "Sort")funcMain () {//Indirect ordering of mapsM: =Map[int]string{1:"a",2:"B",3:"C",4:"D",5:"E"} s: = Make([]int,Len(m)) I: =0     forK, _: =Rangem {S[i] = k i++}//Map has been deposited into sliceFmt. PRINTLN (s)//Each time the print is found to be different, it manifests the disorder of mapSort. Ints (s)//Sort the sliceFmt. PRINTLN (s)//Every time the printed out is ordered    //We can only implement the indirect ordering of maps, because map is unordered, we can only extract post-orderFmt. Println (M)}
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.