Go Structs & Maps

Pointers to structs

v := &Vertex{1, 2} v.X = 2 

Doing v.X is the same as doing (*v).X, when v is a pointer.

Maps

m := make(map[string]int) m["k1"] = 7 m["k2"] = 13 fmt.Println(m) // => map[k1:7 k2:13] v1 := m["k1"] fmt.Println(v1) // => 7 fmt.Println(len(m)) // => 2 delete(m, "k2") fmt.Println(m) // => map[k1:7] _, prs := m["k2"] fmt.Println(prs) // => false n := map[string]int{"foo": 1, "bar": 2} fmt.Println(n) // => map[bar:2 foo:1] 

Literals

v := Vertex{X: 1, Y: 2} // Field names can be omitted v := Vertex{1, 2} // Y is implicit v := Vertex{X: 1} 

You can also put field names.

Defining

package main import ( "fmt" ) type Vertex struct { X int Y int } func main() { v := Vertex{1, 2} v.X = 4 fmt.Println(v.X, v.Y) // => 4 2 } 

See: Structs

Comments