什么是 map?
Map 是 Go 中的內置類型,它將鍵與值綁定到一起。可以通過鍵獲取相應的值。
如何創建 map?
可以通過將鍵和值的類型傳遞給內置函數 make
來創建一個 map。語法為:make(map[KeyType]ValueType)
。(譯者注:map 的類型表示為 map[KeyType]ValueType
)例如:
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 的語法與數組相似。下面的代碼插入一些新的元素給 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]
。
也可以在聲明時初始化一個數組:
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"
為鍵的元素。程序的輸出為:
personSalary map contents: map[steve:12000 jamie:15000 mike:9000]
string
並不是可以作為鍵的唯一類型,其他所有可以比較的類型,比如,布爾類型,整型,浮點型,復數類型都可以作為鍵。如果你想了解更多關於可比較類型的話,請參閱:http://golang.org/ref/spec#Comparison_operators
訪問 map 中的元素
現在我們已經添加了一些元素給 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]
map 的大小
用內置函數 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 是引用類型。當一個 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 的任何修改都會影響在調用函數中看到。