[英]golang - how to initialize a map field within a struct?
我对初始化包含地图的结构的最佳方法感到困惑。 运行此代码会产生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)
}
一个想法是创建一个构造函数,如在这个答案中 。
另一个想法是使用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)
}
还有其他选择吗? 只是想看看是否有一种普遍接受的方式来做到这一点。
我可能会使用构造函数来执行此操作:
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}
}
假设您正确初始化数据结构,代码(尤其是完全由您控制的代码)非常常见。 在这种情况下通常使用结构文字
g := &Graph{
connections: make(map[Vertex][]Vertex),
}
复合文字在构造函数中工作得很好。 使用初始问题(并在地图中天真地存储顶点的副本)来描述一个例子:
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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.