I am doing the go tutorial and I had a question about this exercise... https://tour.golang.org/moretypes/5
I have only briefly worked with pointers and addresses in rudimentary C code before. My understanding is that the line p = &Vertex{1, 2} // has type *Vertex
is pointing a new variable p
to the address of Vertex
.
Wouldn't this then be redefining the definition of the struct
to set X, Y int = 1, 2
Here is the full code from the tutorial:
package main
import "fmt"
type Vertex struct {
X, Y int
}
var (
v1 = Vertex{1, 2} // has type Vertex
v2 = Vertex{X: 1} // Y:0 is implicit
v3 = Vertex{} // X:0 and Y:0
p = &Vertex{1, 2} // has type *Vertex
)
func main() {
fmt.Println(v1, p, v2, v3)
}
var p = &Vertex{1, 2}
does the following:
Vertex
with the values 1 for x
and 2 for y
p
of type *Vertex
(pointer to Vertex
)p
to point to the anonymous variable.It doesn't affect the definition of the type.