"Go Learning Notes" 8, map

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Map

    • Similar to a hash table or dictionary in other languages, storing data in key-value form
    • Key must be a type that supports the = = or! = comparison operation and cannot be a function, map, or slice
    • Map lookup is much faster than linear search, but 100 times times slower than using indexed access data
    • Map using make () to create, support: = This shorthand method

    • Map ([Keytype]valuetype,cap), cap represents capacity, can be omitted

    • Capacity is automatically expanded when exceeded, but as much as possible to provide a reasonable initial value
    • Use Len () to get the number of elements

    • A key-value pair is added automatically when it does not exist, and a key-value pair is deleted using Delete ()

    • Use for range to iterate over map and slice
123456789101112131415161718192021222324252627282930
//k int v stringvarMaptmpMap[int]stringMaptmp = Make(Map[int]string) maptmp[1] ="Hundredlee"Fmt. Println (MAPTMP)//k int v mapvarMaptmpMap[int]Map[int]stringMaptmp = Make(Map[int]Map[int]string) _, OK: = maptmp[0][0]if!ok {maptmp[0] = Make(Map[int]string)}fmt. Println (MAPTMP)//Slice v map + iteratorSlicemap: = Make([]Map[int]string,5) forI: =RangeSlicemap {Slicemap[i] = Make(Map[int]string) slicemap[i][1] ="OK"}fmt. Println (Slicemap)

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.