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) |