package main
import "fmt"
type PersonInfo struct {
Id string
Name string
Address string
}
func main() {
var personDB map[string] PersonInfo
personDB = make(map[string] PersonInfo)
// 往这个map里插入几条数据
personDB["wanglanxin"] = PersonInfo{"1","lanxin", "Room 203"}
personDB["zengjinlin"] = PersonInfo{"2", "zengjinlin", "Room 203"}
person, ok := personDB["zengjinlin"]
if ok {
fmt.Println("Found person", person.Name, "Address is", person.Address)
} else {
fmt.Println("Don't Find This Person")
}
}
/*
声明的格式 var myMap map[string] PersonInfo
myMap: 是声明的map变量名
string: 是声明的键
value: 是存放的类型
创建:myMap = make(map[string] PersonInfo)
赋值:myMap["zengjinlin"] = PersonInfo{"2", "zengjinlin", "Room 203"}
*/
Golang中的Map
最新推荐文章于 2023-06-25 09:59:47 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)