title: How does pointer dereferencing work in Go?
body: <p>I'm going through the golang tutorials at <a href="http://tour.golang.org/" rel="noreferrer">http://tour.golang.org/</a>, and was experimenting a bit with some things in <a href="http://tour.golang.org/#29" rel="noreferrer">example 29</a></p>
  
  <p>For your reference, the original example is copied here:</p>
  
  <pre><code>package main
  
  import "fmt"
  
  type Vertex struct {
      X, Y int
  }
  
  var (
      p = Vertex{1, 2}  // has type Vertex
      q = &amp;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)
  }
  </code></pre>
  
  <p>It's pretty basic, showing how to create instances of this fancy new struct, <code>Vertex</code>.  <a href="http://tour.golang.org/#28" rel="noreferrer">Example 28</a>, 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:</p>
  
  <pre><code>func main() {
      t := *q
      q.X = 4
      u := *q
      fmt.Println(p, q, r, s, t, u, t == u)
  }
  </code></pre>
  
  <p>And the output:</p>
  
  <pre><code>{1 2} &amp;{4 2} {1 0} {0 0} {1 2} {4 2} false
  </code></pre>
  
  <p>The thing that surprised me is that <code>t</code> is not {4, 2}, which seems to mean that changing <code>q.X</code> changed the instance of the struct that <code>q</code> pointed to.  Coming from a C/C++ background, this seems like extremely strange behavior to me.</p>
  
  <p>So, what's actually going on here?  Why does using <code>q.X = 4</code> to change the Vertex not propagate to <code>t</code>?</p>