map
map 的零值为 nil
go
type Vertex struct {
Lat, Long float64
}
var m = map[string]Vertex{
"Bell Labs": Vertex{
40.68433, -74.39967,
},
"Google": Vertex{
37.42202, -122.08408,
},
}
func main() {
fmt.Println(m)
}go
type Vertex struct {
Lat, Long float64
}
// 若顶层类型只是一个类型名,那么你可以在字面量的元素中省略它
var m = map[string]Vertex{
"Bell Labs": {40.68433, -74.39967},
"Google": {37.42202, -122.08408},
}
func main() {
fmt.Println(m)
}crud
go
// 插入 或 修改元素
m[key] = elem
// 获取元素
elem = m[key]
// 删除元素
delete(m, key)
// 是否存在
elem, ok = m[key]