十三.golang的maps
personSalary := make(map[string]int)
上面的代码创建了一个名为 personSalary 的 map,其中键是 string 类型,而值是 int 类型。
map 的零值是 nil。如果你想添加元素到 nil map 中,会触发运行时 panic。因此 map 必须使用 make 函数初始化。
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) } }
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) }
package main import ( "fmt" ) func main() { personSalary := map[string]int { "steve": 12000, "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 employee := "jamie" fmt.Println("Salary of", employee, "is", personSalary[employee]) }
上面的程序很简单。获取并打印员工 jamie 的薪资。程序输出
如果获取一个不存在的元素,会发生什么呢?map 会返回该元素类型的零值。在 personSalary 这个 map 里,如果我们获取一个不存在的元素,会返回 int 类型的零值 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。personSalary 中不包含 joe
如果我们想知道 map 中到底是不是存在这个 key,该怎么做:
value, ok := map[key]
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 行,joe 不存在,所以 ok 是 false。程序将输出:
joe not found
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
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]
package main import ( "fmt" ) func main() { personSalary := map[string]int{ "steve": 12000, "jamie": 15000, } personSalary["mike"] = 9000 fmt.Println("length is", len(personSalary)) }
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) }
Original person salary map[steve:12000 jamie:15000 mike:9000]
Person salary changed map[steve:12000 jamie:15000 mike:18000]
当 map 作为函数参数传递时也会发生同样的情况。函数中对 map 的任何修改,对于外部的调用都是可见的。
package main func main() { map1 := map[string]int{ "one": 1, "two": 2, } map2 := map1 if map1 == map2 { } }
上面程序抛出编译错误 invalid operation: map1 == map2 (map can only be compared to nil)。