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}
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 ?
pointers go
Kevin
source share