title: How to get a pointer to the underlying value of an Interface{} in Go
body: <p>I'm interfacing with C code in Go using cgo, and I need to call a C function with a pointer to the underlying value in an <code>Interface{}</code> object. The value will be any of the atomic primitive types (not including <code>complex64</code>/<code>complex128</code>), or <code>string</code>.</p>
  <p>I was hoping I'd be able to do something like this to get the address of <code>ptr</code> as an <code>unsafe.Pointer</code>:</p>
  <pre><code>unsafe.Pointer(reflect.ValueOf(ptr).UnsafeAddr())
  </code></pre>
  <p>But this results in a panic due to the value being unaddressable.</p>
  <p>A similar question to this is <a href="https://stackoverflow.com/questions/15518919/take-address-of-value-inside-an-interface">Take address of value inside an interface</a>, but this question is different, as in this case it is known that the value will always be one of the types specified above (which will be at most 64 bits), and I only need to give this value to a C function. Note that there are multiple C functions, and the one that will be called varies based off of a different unrelated parameter.</p>
  <p>I also tried to solve this using a type switch statement, however I found myself unable to get the address of the values even after the type assertion was done. I was able to assign the values to temporary copies, then get the address of those copies, but I'd rather avoid making these copies if possible.</p>