I'm confused about the best way to initialize a struct that contains a map. Running this code produces 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)
}
One idea is to create a constructor, as in this answer.
Another idea is to use an add_connection
method that can initialize the map if it's empty:
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)
}
Are there other options? Just wanted to see if there is a commonly-accepted way to do this.
Initializing map using map literals: Map literal is the easiest way to initialize a map with data just simply separate the key-value pair with a colon and the last trailing colon is necessary if you do not use, then the compiler will give an error.
The Static Initializer for a Static HashMap We can also initialize the map using the double-brace syntax: Map<String, String> doubleBraceMap = new HashMap<String, String>() {{ put("key1", "value1"); put("key2", "value2"); }};
Go by Example: Maps To create an empty map, use the builtin make : make(map[key-type]val-type) . Set key/value pairs using typical name[key] = val syntax. Printing a map with e.g. fmt. Println will show all of its key/value pairs.
What exactly do you want to initialize to zero? map's default constructor creates empty map. You may increase map's size only by inserting elements (like m["str1"]=0 or m. insert(std::map<std::string,int>::value_type("str2",0)) ).
I would probably use a constructor to do this:
func NewGraph() *Graph { var g Graph g.connections = make(map[Vertex][]Vertex) return &g }
I've found this example in the standard image/jpeg
package (not with a map though, but with a slice):
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} }
It's very common for code (especially code fully under your control) to assume you initialize the data structure correctly. A struct literal is usually used in this case
g := &Graph{
connections: make(map[Vertex][]Vertex),
}
Composite literals work just fine inside a constructor. Contriving an example using the initial question (and naively storing copies of Vertices in the map):
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)
}
https://play.golang.org/p/Lf4Gomp4tJ
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With