title: Do pointers change value in Go due to garbage collection?
body: <p>I'm a relative newbie to go.  I have been working a lot with cgo and building an interface to a C api.  The C api uses a registered callback and a <code>void*</code> cookie parameter to the callback.</p>
  
  <p>Here is some code exemplifying my problem:</p>
  
  <pre><code>/*
  void cb(void *cookie) {
     callGo(cookie);
  }
  
  go_register_cb(void *ptr) {
     register_cb(&amp;cb, ptr);
  }
  */
  import "C"
  </code></pre>
  
  <p>and</p>
  
  <pre><code>import "C"
  
  //export callGo
  callGo(ptr unsafe.Pointer) {
      x := (*MyStruct)(ptr)
      x.doSomething()
  }
  
  func (x *MyStruct) Start() {
      C.go_register_cb(unsafe.Pointer(x))
  }
  </code></pre>
  
  <p>This code causes a runtime error about Go pointers to Go pointers.</p>
  
  <p>I think I understand the basic implications of keeping Go pointer inside C-land, which is that the C-land reference will not be accounted for in the go garbage collector so C may end up using a stale reference.</p>
  
  <p>The specific requirement that I cannot pass a Go pointer to a data-structure that itself contains a Go pointer is more puzzling. The only thing that makes sense to me is that pointers in Go may change value (that is, the address they point to) due to garbage collection. Is this the case?</p>