Map 是 Go 中的內置類型,它將鍵與值綁定到一塊兒。能夠經過鍵獲取相應的值。golang
能夠經過將鍵和值的類型傳遞給內置函數 make
來建立一個 map。語法爲:make(map[KeyType]ValueType)
。(譯者注:map 的類型表示爲 map[KeyType]ValueType
)例如:c#
personSalary := make(map[string]int)
上面的代碼建立了一個名爲 personSalary
的 map。其中鍵的類型爲 string,值的類型爲 int。數組
map 的 0 值爲 nil。試圖給一個 nil map 添加元素給會致使運行時錯誤。所以 map 必須經過 make
來初始化(譯者注:也可使用速記聲明來建立 map,見下文)。數據結構
package main import ( "fmt" ) func main() { var personSalary map[string]int if personSalary == nil { fmt.Println("map is nil. Going to make one.") personSalary = make(map[string]int) } }
上面的程序中,personSalary
爲 nil,所以使用 make
初始化它。程序的輸出爲:map is nil. Going to make one.
函數
插入元素給 map 的語法與數組類似。下面的代碼插入一些新的元素給 map personSalary
。學習
package main import ( "fmt" ) func main() { personSalary := make(map[string]int) personSalary["steve"] = 12000 personSalary["jamie"] = 15000 personSalary["mike"] = 9000 fmt.Println("personSalary map contents:", personSalary) }
上面的程序輸出:personSalary map contents: map[steve:12000 jamie:15000 mike:9000]
。ui
也能夠在聲明時初始化一個數組:spa
package main import ( "fmt" ) func main() { personSalary := map[string]int { "steve": 12000, "jamie": 15000, } personSalary["mike"] = 9000 fmt.Println("personSalary map contents:", personSalary) }
上面的程序在聲明 personSalary
的同時向其中插入了兩個元素。接着插入了一個以 "mike"
爲鍵的元素。程序的輸出爲:.net
personSalary map contents: map[steve:12000 jamie:15000 mike:9000]
string
並非能夠做爲鍵的惟一類型,其餘全部能夠比較的類型,好比,布爾類型,整型,浮點型,複數類型均可以做爲鍵。若是你想了解更多關於可比較類型的話,請參閱:http://golang.org/ref/spec#Comparison_operatorscode
如今咱們已經添加了一些元素給 map,如今讓咱們學習如何從 map 中提取它們。根據鍵獲取值的語法爲:map[key]
,例如:
package main import ( "fmt" ) func main() { personSalary := map[string]int{ "steve": 12000, "jamie": 15000, } personSalary["mike"] = 9000 employee := "jamie" fmt.Println("Salary of", employee, "is", personSalary[employee]) }
上面的程序很是簡單。員工 jamie
的工資被取出並打印。程序的輸出爲:Salary of jamie is 15000
。
若是一個鍵不存在會發生什麼?map 會返回值類型的 0 值。好比若是訪問了 personSalary
中的不存在的鍵,那麼將返回 int 的 0 值,也就是 0。
package main import ( "fmt" ) func main() { personSalary := map[string]int{ "steve": 12000, "jamie": 15000, } personSalary["mike"] = 9000 employee := "jamie" fmt.Println("Salary of", employee, "is", personSalary[employee]) fmt.Println("Salary of joe is", personSalary["joe"]) }
上面的程序輸出爲:
Salary of jamie is 15000 Salary of joe is 0
上面的程序返回 joe
的工資爲 0。咱們沒有獲得任何運行時錯誤說明鍵 joe
在 personSalary
中不存在。
咱們如何檢測一個鍵是否存在於一個 map 中呢?可使用下面的語法:
value, ok := map[key]
上面的語法能夠檢測一個特定的鍵是否存在於 map 中。若是 ok
是 true,則鍵存在,value 被賦值爲對應的值。若是 ok
爲 false,則表示鍵不存在。
package main import ( "fmt" ) func main() { personSalary := map[string]int{ "steve": 12000, "jamie": 15000, } personSalary["mike"] = 9000 newEmp := "joe" value, ok := personSalary[newEmp] if ok == true { fmt.Println("Salary of", newEmp, "is", value) } else { fmt.Println(newEmp,"not found") } }
在上面的程序中,第 15 行,ok
應該爲 false 由於 joe
不存在。所以程序的輸出爲:
joe not found
range for 可用於遍歷 map 中全部的元素(譯者注:這裏 range 操做符會返回 map 的鍵和值)。
package main import ( "fmt" ) func main() { personSalary := map[string]int{ "steve": 12000, "jamie": 15000, } personSalary["mike"] = 9000 fmt.Println("All items of a map") for key, value := range personSalary { fmt.Printf("personSalary[%s] = %d\n", key, value) } }
上面的程序輸出以下:
All items of a map personSalary[mike] = 9000 personSalary[steve] = 12000 personSalary[jamie] = 15000
值得注意的是,由於 map 是無序的,所以對於程序的每次執行,不能保證使用 range for 遍歷 map 的順序老是一致的。
delete(map, key)
用於刪除 map 中的 key。delete
函數沒有返回值。
package main import ( "fmt" ) func main() { personSalary := map[string]int{ "steve": 12000, "jamie": 15000, } personSalary["mike"] = 9000 fmt.Println("map before deletion", personSalary) delete(personSalary, "steve") fmt.Println("map after deletion", personSalary) }
上面的程序刪除以 steve
爲鍵的元素。程序輸出爲:
map before deletion map[steve:12000 jamie:15000 mike:9000] map after deletion map[mike:9000 jamie:15000]
用內置函數 len 獲取 map 的大小:
package main import ( "fmt" ) func main() { personSalary := map[string]int{ "steve": 12000, "jamie": 15000, } personSalary["mike"] = 9000 fmt.Println("length is", len(personSalary)) }
上面程序中,len(personSalary)
獲取 personSalary
的大小。上面的程序輸出:length is 3
。
與切片同樣,map 是引用類型。當一個 map 賦值給一個新的變量,它們都指向同一個內部數據結構。所以改變其中一個也會反映到另外一個:
package main import ( "fmt" ) func main() { personSalary := map[string]int{ "steve": 12000, "jamie": 15000, } personSalary["mike"] = 9000 fmt.Println("Original person salary", personSalary) newPersonSalary := personSalary newPersonSalary["mike"] = 18000 fmt.Println("Person salary changed", personSalary) }
上面的程序中,第 14 行,personSalary
賦值給 newPersonSalary
。下一行,將 newPersonSalary
中 mike
的工資改成 18000
。那麼在 personSalary
中 mike
的工資也將變爲 18000
。程序的輸出以下:
Original person salary map[steve:12000 jamie:15000 mike:9000] Person salary changed map[jamie:15000 mike:18000 steve:12000]
將 map 做爲參數傳遞給函數也是同樣的。在函數中對 map 的任何修改都會影響在調用函數中看到。