How does pointer dereferencing work in Go?

I study golang tutorials at http://tour.golang.org/ and experimented a bit with some things in Example 29

For 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) } 

This is a fairly simple example showing how to instantiate this fantastic new structure, Vertex . Example 28 , however, shows manipulating a vertex with a pointer to it, so I changed the example a bit and was surprised at the exit. Here is the modification:

 func main() { t := *q qX = 4 u := *q fmt.Println(p, q, r, s, t, u, t == u) } 

And the conclusion:

 {1 2} &{4 2} {1 0} {0 0} {1 2} {4 2} false 

I was surprised that t not {4, 2}, which apparently means that changing qX changed the instance of the structure q points to. Based on the background of C / C ++, this seems to me a very strange behavior.

So what is really going on here? Why does not using qX = 4 to change Tops extend to t ?

+17
pointers go
source share
1 answer

t := *q creates a copy of the structure q points to.

If you want to observe changes in q through t , then stick to the pointer:

 func main() { t := q qX = 4 u := *q fmt.Println(p, q, r, s, t, u, *t == u) } 

This will lead to what you were probably looking for.

 {1 2} &{4 2} {1 0} {0 0} &{4 2} {4 2} true 

I'm not sure what you think is extremely strange. C and C ++ behave the same. Consider the following:

 #include <iostream> struct Vertex { int x; int y; }; std::ostream& operator<<(std::ostream& out, const Vertex& v) { out << "{ " << vx << ", " << vy << " }"; return out; } int main() { Vertex v = Vertex{1, 2}; Vertex* q = &v; Vertex t = *q; q->x = 4; std::cout << "*q: " << *q << "\n"; std::cout << " t: " << t << "\n"; } 

The result of this C ++ code shows the same behavior:

 *q: { 4, 2 } t: { 1, 2 } 
+28
source share

All Articles