I'm going through the golang tutorials at http://tour.golang.org/, and was experimenting a bit with some things in example 29
For your reference, the original example is copied here:
package main
import "fmt"
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:0 is implicit
s = Vertex{} // X:0 and Y:0
)
func main() {
fmt.Println(p, q, r, s)
}
It's pretty basic, showing how to create instances of this fancy new struct, Vertex
. Example 28, though, shows manipulating a vertex via a pointer to it, so I modified the example a bit and was surprised by the output. Here is the modification:
func main() {
t := *q
q.X = 4
u := *q
fmt.Println(p, q, r, s, t, u, t == u)
}
And the output:
{1 2} &{4 2} {1 0} {0 0} {1 2} {4 2} false
The thing that surprised me is that t
is not {4, 2}, which seems to mean that changing q.X
changed the instance of the struct that q
pointed to. Coming from a C/C++ background, this seems like extremely strange behavior to me.
So, what's actually going on here? Why does using q.X = 4
to change the Vertex not propagate to t
?
t := *q
makes a copy of the struct pointed to by q
.
If you want to observe changes to q
through t
, then stick with a pointer:
func main() {
t := q
q.X = 4
u := *q
fmt.Println(p, q, r, s, t, u, *t == u)
}
This produces the output you were probably looking for.
{1 2} &{4 2} {1 0} {0 0} &{4 2} {4 2} true
I'm not sure what seems extremely strange to you. C and C++ behave the same way. Consider the following:
#include <iostream>
struct Vertex
{
int x;
int y;
};
std::ostream& operator<<(std::ostream& out, const Vertex& v)
{
out << "{ " << v.x << ", " << v.y << " }";
return out;
}
int main()
{
Vertex v = Vertex{1, 2};
Vertex* q = &v;
Vertex t = *q;
q->x = 4;
std::cout << "*q: " << *q << "
";
std::cout << " t: " << t << "
";
}
The output of this C++ code shows the same behavior:
*q: { 4, 2 }
t: { 1, 2 }