In case of Struct Literals in Go,
type Vertex struct {
X, Y int
}
var (
p = Vertex{1, 2} // has type Vertex
q = &Vertex{1, 2} // has type *Vertex
r = Vertex{X: 1, Y: 2}
)
The values for p, q and r are {1 2} &{1 2} {1 2}
What is the difference between the initialisation methods of the above three variables ? How are the variables p, q and r different ?
q
is a pointer to a struct allocated on the heap. The others are identical, and allocated on the stack. Whether you list the field names is purely for readability, and I suggest doing so whenever possible.