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

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.

主站蜘蛛池模板: 白城市| 榆社县| 禹州市| 桦甸市| 湖州市| 通化县| 乌什县| 宿松县| 阳原县| 镇安县| 汉源县| 济南市| 南京市| 宽甸| 石棉县| 涞水县| 盐源县| 东乌珠穆沁旗| 万州区| 乌拉特后旗| 腾冲县| 普陀区| 南京市| 浦城县| 水富县| 奇台县| 铁岭市| 应城市| 额敏县| 泾源县| 农安县| 枣庄市| 海阳市| 宣城市| 博爱县| 丹棱县| 保靖县| 盘山县| 云龙县| 石棉县| 东至县|