在 Go 中,map 数据结构以键/值对的形式存储元素。在这里,键是与 map 中的每个值关联的唯一标识符。
在 Golang 中创建 map
创建 Go map 的语法是
subjectMarks := map[string]float32{"Golang": 85, "Java": 80, "Python": 81}
此代码创建了一个名为 subjectMarks 的 map。在这里,
[string]
- 表示 map 的键是 string 类型float32
- 表示 map 的值是 float 类型{Golang", "Java", "Python"}
- map 的键{85, 80, 81}
- map 的值
示例:Golang 中的 Map
// Program to create a map and print its keys and values
package main
import "fmt"
func main() {
// create a map
subjectMarks := map[string]float32{"Golang": 85, "Java": 80, "Python": 81}
fmt.Println(subjectMarks)
}
输出
map[Golang:85 Java:80 Python:81]
在这里,我们创建了一个 map 并打印了 map 的键值。
注意:我们也可以使用 var
关键字创建 map。例如,
var subjectMarks = map[string]float32{"Golang": 85, "Java": 80, "Python": 81}
在 Golang 中访问 Map 的值
我们可以使用相应的键来访问 map 的值。例如,
package main
import "fmt"
func main() {
// create a map
flowerColor := map[string]string{"Sunflower": "Yellow", "Jasmine": "White", "Hibiscus": "Red"}
// access value for key Sunflower
fmt.Println(flowerColor["Sunflower"]) // Yellow
// access value for key Hibiscus
fmt.Println(flowerColor["Hibiscus"]) // Red
}
在这里,我们使用了表达式
flowerColor["Sunflower"]
来访问键Sunflower
的值flowerColor["Hibiscus"]
来访问键Hibiscus
的值
在 Golang 中更改 Map 的值
要更改 map 的值,我们可以直接为相应的键赋一个新值。例如,
package main
import "fmt"
func main() {
// create a map
capital := map[string]string{ "Nepal": "Kathmandu", "US": "New York"}
fmt.Println("Initial Map: ", capital)
// change value of US to Washington DC
capital["US"] = "Washington DC"
fmt.Println("Updated Map: ", capital)
}
输出
Initial Map: map[Nepal: Kathmandu US: New York] Updated Map: map[Nepal: Kathmandu US: Washington DC]
在上面的示例中,我们通过将键 "US" 重新赋值为新值 "Washington DC" 来更改了 map 的值。
添加 Go Map 元素
到目前为止,我们已经创建了一个包含预定义元素的 map。但是,我们也可以向 map 添加元素。
要添加元素,我们可以分配一个带有新键的新值。例如,
package main
import "fmt"
func main() {
// create a map
students := map[int]string{1: "John", 2: "Lily"}
fmt.Println("Initial Map: ", students)
// add element with key 3
students[3] = "Robin"
// add element with key 5
students[5] = "Julie"
fmt.Println("Updated Map: ", students)
}
输出
Initial Map: map[1:John 2:Lily] Updated Map: map[1:John 2:Lily 3:Robin 5:Julie]
这里,代码
students[3] = "Robin"
- 添加了一个键为 3,值为Robin
的新元素students[5] = "Julie"
- 添加了一个键为 5,值为Julie
的新元素
删除 Go Map 元素
要删除 map 的元素,我们可以使用 delete()
函数。例如,
package main
import "fmt"
func main() {
// create a map
personAge := map[string]int{"Hermione": 21, "Harry": 20, "John": 25}
fmt.Println("Initial Map: ", personAge)
// remove element of map with key John
delete(personAge, "John")
fmt.Println("Updated Map: ", personAge)
}
输出
Initial Map: map[Harry:20 Hermione:21 John:25] Updated Map: map[Harry:20 Hermione:21]
在这里,我们使用 delete()
函数删除了由键 "John" 表示的元素。
delete(personAge, "John")
该函数接受两个参数
personAge
- map 的名称John
- 要删除的元素的键
注意:如果传递给 delete()
函数的键不存在于 map 中,则该函数不执行任何操作。
在 Golang 中遍历 map
我们可以使用 Go for range 循环 来迭代 map 的每个元素。例如,
package main
import "fmt"
func main() {
// create a map
squaredNumber := map[int]int{2: 4, 3: 9, 4: 16, 5: 25}
// for-range loop to iterate through each key-value of map
for number, squared := range squaredNumber {
fmt.Printf("Square of %d is %d\n", number, squared)
}
}
输出
Square of 2 is 4 Square of 3 is 9 Square of 4 is 16 Square of 5 is 25
在上面的代码中,for range 循环访问了 map 的每个键/值对。
for range 循环的工作原理
迭代 | number | squared |
---|---|---|
1 | 2 | 4 |
2 | 3 | 9 |
3 | 4 | 16 |
4 | 5 | 25 |
注意:我们使用了 Printf() 函数,使我们的输出更清晰易懂。
常见问题
到目前为止,我们在创建 map 时都提供了初始值。但是,如果我们想创建一个没有任何初始值的 map,我们可以使用 make()
函数。
package main
import "fmt"
func main() {
// create a map using make()
student := make(map[int]string)
// add elements to the map
student[1] = "Harry"
student[2] = "Lilly"
student[5] = "Harmonie"
fmt.Println(student)
}
输出
map[1:Harry 2:Lilly 5:Harmonie]
一旦我们使用 make() 函数创建了 map,我们就可以对 map 执行所有其他操作(更改、删除、访问)。
我们还可以使用 for range
来仅访问 map 的键。例如,
package main
import "fmt"
func main() {
// create a map
places := map[string]string{"Nepal": "Kathmandu", "US": "Washington DC", "Norway": "Oslo"}
// access only the keys of the map
for country := range places {
fmt.Println(country)
}
}
输出
Nepal US Norway
注意 for range 循环的用法,
for country := range places {
在这里,第二个实体(capital)没有被使用,因为我们只想检索 map 的键 "Nepal"、"US"、"Norway"。
我们使用空标识符 _
配合 for range 循环来访问 map 的值。例如,
package main
import "fmt"
func main() {
places := map[string]string{"Nepal": "Kathmandu", "US": "Washington DC", "Norway": "Oslo"}
for _, capital := range places {
fmt.Println(capital)
}
}
输出
Kathmandu Washington DC Oslo
请注意,我们在 country(表示键的变量)的位置使用了 _
。这是因为如果我们声明了任何变量,就必须在 for 循环体中使用它们。
所以,如果我们使用了 country
(用于表示键的变量),我们就必须在循环中使用它。否则,我们会收到一个错误。
places := map[string]string{"Nepal": "Kathmandu", "US": "Washington DC", "Norway": "Oslo"}
// country variable is declared but not used
for country, capital := range places {
// throws an error
fmt.Println(capital)
}