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) personDB["12345"] = PersonInfo{"12345", "Tom", "Room 203..."} personDB["1"] = PersonInfo{"1", "Jack", "Room 1..."} person, ok := personDB["12345"] if ok { fmt.Println("Fond person", person.Name, "with ID 1234.") } else { fmt.Println("Did not find person with ID1234.") } }
D:/gocode/gocode.exe [D:/gocode]
Did not find person with ID1234.
成功: 进程退出代码 0.
c:/go/bin/go.exe build [D:/gocode]
成功: 进程退出代码 0.
D:/gocode/gocode.exe [D:/gocode]
Fond person Tom with ID 1234.
成功: 进程退出代码 0.