我对初始化包含地图的结构的最佳方法感到困惑。运行此代码会生成panic: runtime error: assignment to entry in nil map
:
package main
type Vertex struct {
label string
}
type Graph struct {
connections map[Vertex][]Vertex
}
func main() {
v1 := Vertex{"v1"}
v2 := Vertex{"v2"}
g := new(Graph)
g.connections[v1] = append(g.coonections[v1], v2)
g.connections[v2] = append(g.connections[v2], v1)
}
一个想法是创建一个构造函数,如this answer。
另一个想法是使用add_connection
方法,如果地图为空,可以初始化地图:
func (g *Graph) add_connection(v1, v2 Vertex) {
if g.connections == nil {
g.connections = make(map[Vertex][]Vertex)
}
g.connections[v1] = append(g.connections[v1], v2)
g.connections[v2] = append(g.connections[v2], v1)
}
还有其他选择吗?只是想看看是否有一种普遍接受的方式来做到这一点。
答案 0 :(得分:33)
我可能会使用构造函数来执行此操作:
func NewGraph() *Graph {
var g Graph
g.connections = make(map[Vertex][]Vertex)
return &g
}
我在标准image/jpeg
包中找到了这个示例(虽然没有地图,但有切片):
type Alpha struct {
Pix []uint8
Stride int
Rect Rectangle
}
func NewAlpha(r Rectangle) *Alpha {
w, h := r.Dx(), r.Dy()
pix := make([]uint8, 1*w*h)
return &Alpha{pix, 1 * w, r}
}
答案 1 :(得分:13)
代码(特别是完全在您控制之下的代码)假设您正确初始化数据结构非常常见。在这种情况下通常使用结构文字
g := &Graph{
connections: make(map[Vertex][]Vertex),
}
答案 2 :(得分:2)
复合文字在构造函数中工作得很好。使用初始问题(并在地图中天真地存储顶点的副本)来描述一个例子:
func NewGraph(v1 Vertex, v2 Vertex) *Graph {
return &Graph{ map[Vertex][]Vertex{ v1: []Vertex{v2}, v2: []Vertex{v1} }}
}
func main() {
v1 := Vertex{"v1"}
v2 := Vertex{"v2"}
g := NewGraph(v1, v2)
fmt.Println(g)
}