- Go Systems Programming
- Mihalis Tsoukalos
- 560字
- 2021-07-02 18:07:59
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.
- HTML5+CSS3王者歸來(lái)
- OpenStack Cloud Computing Cookbook(Third Edition)
- The Supervised Learning Workshop
- LabVIEW2018中文版 虛擬儀器程序設(shè)計(jì)自學(xué)手冊(cè)
- SpringMVC+MyBatis快速開發(fā)與項(xiàng)目實(shí)戰(zhàn)
- 基于Java技術(shù)的Web應(yīng)用開發(fā)
- 算法大爆炸:面試通關(guān)步步為營(yíng)
- 64位匯編語(yǔ)言的編程藝術(shù)
- Monitoring Elasticsearch
- 用Python實(shí)現(xiàn)深度學(xué)習(xí)框架
- Rust游戲開發(fā)實(shí)戰(zhàn)
- PhoneGap 4 Mobile Application Development Cookbook
- Python硬件編程實(shí)戰(zhàn)
- After Effects CC案例設(shè)計(jì)與經(jīng)典插件(視頻教學(xué)版)
- Java語(yǔ)言程序設(shè)計(jì)實(shí)用教程(第2版)