Golang 中的映射是无序键值对的集合。它被广泛使用,因为它提供了快速查找和值,可以在键的帮助下检索、更新或删除。
句法:
map[Key_Type]Value_Type{}
示例: var sample map[字符串]int
这里的示例是一个映射,它以字符串为键,以 int 类型为值。
在映射中,大多数数据类型都可以用作键,如 int、 字符串、float64、rune 等。映射还允许将结构用作键。这些结构应该相互比较。 Golang 中的结构体或结构体是用户定义的类型,它允许将不同类型的字段组合成单一类型。
结构体示例:
type Student struct {
name string
rollno int
class string
city string
}
让我们看看如何在地图中实现一个结构体:
示例 1:
// Golang program to show how to
// use structs as map keys
package main
// importing required packages
import "fmt"
//declaring a struct
type Address struct {
Name string
city string
Pincode int
}
func main() {
// Creating struct instances
a2 := Address{Name: "Ram", city: "Delhi", Pincode: 2400}
a1 := Address{"Pam", "Dehradun", 2200}
a3 := Address{Name: "Sam", city: "Lucknow", Pincode: 1070}
// Declaring a map
var mp map[Address]int
// Checking if the map is empty or not
if mp == nil {
fmt.Println("True")
} else {
fmt.Println("False")
}
// Declaring and initialising
// using map literals
sample := map[Address]int{a1: 1, a2: 2, a3: 3}
fmt.Println(sample)
}
输出:
True
map[{Pam Dehradun 2200}:1 {Ram Delhi 2400}:2 {Sam Lucknow 1070}:3]
迭代地图:您还可以运行循环来单独访问和操作每个地图键。
示例 2:
// Golang program to show how to
// use structs as map keys
package main
// importing required packages
import "fmt"
// declaring a struct
type Address struct {
Name string
city string
Pincode int
}
func main() {
// Creating struct instances
a1 := Address{"Pam", "Mumbai", 2200}
a2 := Address{Name: "Ram", city: "Delhi", Pincode: 2400}
a3 := Address{Name: "Sam", city: "Lucknow", Pincode: 1070}
// Declaring and initialising using map literals
sample := map[Address]int{a1: 1, a2: 2, a3: 3}
for str, val := range sample {
fmt.Println(str, val)
}
// You can also access a struct
// field while using a loop
for str := range sample {
fmt.Println(str.Name)
}
}
输出:
{Ram Delhi 2400} 2
{Sam Lucknow 1070} 3
{Pam Mumbai 2200} 1
Pam
Ram
Sam
在地图中添加键:值对:使用给定的语法在地图中添加键:值对:
map_name[struct_instance]=value
如果映射中已经存在键值对,它只会用新的键值对更新旧的键值对。
示例 3:
// Adding key:value pair in a map
package main
// importing required packages
import "fmt"
// declaring a struct
type Student struct {
Name string
rollno int
course string
}
func main() {
// Creating struct instances
a1 := Student{"Asha", 1, "CSE"}
a2 := Student{"Aishwarya", 1, "ECE"}
a3 := Student{"Priya", 2, "MECH"}
// Declaring and initialising
// using map literals
mp := map[Student]int{a1: 1, a2: 2}
fmt.Println("Original map was", mp)
mp[a3] = 3
mp[Student{"Ram", 3, "CSE"}] = 4
// Values have their zero values
// Here initial value was 0 after
// incrementing it became 1
mp[Student{"Tina", 44, "EEE"}]++
fmt.Println("After adding key:value "+
"pairs to the map, Updated map is:", mp)
}
输出:
Original map was map[{Aishwarya 1 ECE}:2 {Asha 1 CSE}:1]
After adding key:value pairs to the map, Updated map is: map[{Aishwarya 1 ECE}:2 {Asha 1 CSE}:1 {Priya 2 MECH}:3 {Ram 3 CSE}:4 {Tina 44 EEE}:1]
从映射中删除结构键:您可以使用 delete()函数从映射中删除结构键。它是一个内置函数,不返回任何值,如果给定映射中不存在键,则不执行任何操作。相同的语法如下:
delete(map_name, struct_key)
示例 4:
// Deleting key: value pair in a map
package main
// importing required packages
import "fmt"
// declaring a struct
type Student struct {
Name string
rollno int
course string
}
func main() {
// Creating struct instances
a1 := Student{"Asha", 1, "CSE"}
a2 := Student{"Aishwarya", 1, "ECE"}
a3 := Student{"Priya", 2, "MECH"}
a4 := Student{"Ram", 3, "CSE"}
// Declaring and initialising using map literals
mp := map[Student]int{a1: 1, a2: 2, a3: 3, a4: 4}
delete(mp, a4)
fmt.Println("The remaining map after deletion:")
for str, i := range mp {
fmt.Println(str, "=", i)
}
}
输出:
The remaining map after deletion:
{Asha 1 CSE} = 1
{Aishwarya 1 ECE} = 2
{Priya 2 MECH} = 3
检查键:值对是否存在:您可以检查映射中是否存在结构。下面给出了检查映射中 struct_key: 值对是否存在的语法:
// This gives the value and check result
// If the check result is True, it means the key is present
// If the check result is False, it means the key is missing and in that case value takes a zero value
value, check_variable_name:= map_name[key]
or
// Without value using the blank identifier
// It will only give check result
_, check_variable_name:= map_name[key]
示例 6:
// Golang program to check if a
// struct key is present
package main
// importing required packages
import "fmt"
// declaring a struct
type Student struct {
Name string
rollno int
course string
}
func main() {
// Creating struct instances
a1 := Student{"Asha", 1, "CSE"}
a2 := Student{"Aishwarya", 1, "ECE"}
a3 := Student{"Priya", 2, "MECH"}
a4 := Student{"Ram", 3, "CSE"}
// Declaring and initialising
// using map literals
mp := map[Student]string{a1: "First",
a2: "Second", a3: "Third", a4: "Fourth"}
value, check := mp[a4]
fmt.Println("Is the key present:", check)
fmt.Println("Value of the key:", value)
_, check2 := mp[a2]
fmt.Println("Is the key present:", check2)
}
输出:
Is the key present: true
Value of the key: Fourth
Is the key present: true