官术网_书友最值得收藏!

Maps

The Map data type in Go is equivalent to the well-known hash table found in other programming languages. The main advantage of maps is that they can use almost any data type as their index, which in this case is called a key. For a data type to be used as a key, it must be comparable.

So, let's take a look at an example Go program, named maps.go, which we will use for illustrative purposes. The first part of maps.go contains the preamble Go code you would expect:

package main 
 
import ( 
   "fmt" 
) 
 
func main() { 
 

Then, you can define a new empty map that has strings as its keys and integer numbers as values, as follows:

   aMap := make(map[string]int) 

Post this, you can add new key and value pairs to the aMap map, as follows:

   aMap["Mon"] = 0 
   aMap["Tue"] = 1 
   aMap["Wed"] = 2 
   aMap["Thu"] = 3 
   aMap["Fri"] = 4 
   aMap["Sat"] = 5 
   aMap["Sun"] = 6 

Then, you can get the value of an existing key:

   fmt.Printf("Sunday is the %dth day of the week.\n", aMap["Sun"]) 
 

However, the single most important operation you can perform on an existing map is illustrated in the following Go code:

   _, ok := aMap["Tuesday"] 
   if ok { 
         fmt.Printf("The Tuesday key exists!\n") 
   } else { 
         fmt.Printf("The Tuesday key does not exist!\n") 
   } 

What the aforementioned Go code does is use the error-handling capabilities of Go in order to verify that a key of a map already exists before you try to get its value. This is the proper and safe way of trying to get the value of a map key because asking for a value for which there is no key will result in returning zero. This gives you no way of determining whether the result was zero because the key you requested was not there or because the element with the corresponding key actually had the zero value.

The following Go code shows how you can iterate over all the keys of an existing map:

   count := 0 
   for key, _ := range aMap { 
         count++ 
         fmt.Printf("%s ", key) 
   } 
   fmt.Printf("\n") 
   fmt.Printf("The aMap has %d elements\n", count) 

If you have no interest in visiting the keys and the values of a map and you just want to count its pairs, then you can use the next, much simpler variation of the previous for loop:

   count = 0 
   delete(aMap, "Fri") 
   for _, _ = range aMap { 
         count++ 
   } 
   fmt.Printf("The aMap has now %d elements\n", count) 

The last part of the main() function contains the following Go code that illustrates an alternative way of defining and initializing a map at the same time:

   anotherMap := map[string]int{ 
         "One":   1, 
         "Two":   2, 
         "Three": 3, 
         "Four":  4, 
   } 
anotherMap["Five"] = 5 count = 0 for _, _ = range anotherMap { count++ } fmt.Printf("anotherMap has %d elements\n", count) }

However, apart from the different initialization, all the other map operations work exactly the same. Executing maps.go generates the following output:

$ go run maps.go
Sunday is the 6th day of the week.
The Tuesday key does not exist!
Wed Thu Fri Sat Sun Mon Tue
The aMap has 7 elements
The aMap has now 6 elements
anotherMap has 5 elements

Maps are a very handy data structure, and there is a big chance that you are going to need them when developing systems software.

主站蜘蛛池模板: 泰顺县| 贵南县| 周宁县| 江山市| 吉木萨尔县| 肥乡县| 南岸区| 朔州市| 尼勒克县| 威信县| 驻马店市| 小金县| 奎屯市| 太和县| 前郭尔| 双鸭山市| 邯郸县| 穆棱市| 格尔木市| 巩义市| 宜都市| 水城县| 曲阳县| 隆化县| 象州县| 敦煌市| 中西区| 万宁市| 顺昌县| 吴川市| 顺平县| 高碑店市| 明水县| 密山市| 长兴县| 乐清市| 额尔古纳市| 临泉县| 津南区| 杭锦后旗| 磐安县|