title: cgo C struct field access from Go: underscore or no underscore?
body: <p>I'm running into a disconnect between the online documentation and the behavior I see in my programs accessing C structs within GO code. <code>go version</code> says I am using:</p>
  
  <pre><code>go version go1.4.2 linux/amd64
  </code></pre>
  
  <p>According to the <a href="https://golang.org/cmd/cgo/" rel="nofollow">GO CGO documentation</a>: </p>
  
  <blockquote>
    <p>Within the Go file, C's struct field names that are keywords in Go can be 
    accessed by prefixing them with an underscore: if x points at a C struct with 
    a field named "type", x._type accesses the field. C struct fields that cannot 
    be expressed in Go, such as bit fields or misaligned data, are omitted in the 
    Go struct, replaced by appropriate padding to reach the next field or the end 
    of the struct.</p>
  </blockquote>
  
  <p>I had troubles with this, so made a quick sample program to test it out:</p>
  
  <pre><code>package main
  // struct rec
  // {
  //      int    i;
  //      double d;
  //      char*  s;
  // };
  import "C"
  import "fmt"
  func main() {
          s := "hello world"
          r := C.struct_rec{}
          r.i = 9
          r.d = 9.876
          r.s = C.CString(s)
          fmt.Printf("\n\tr.i: %d\n\tr.d: %f\n\tr.s: %s\n", 
                  r.i, 
                  r.d, 
                  C.GoString(r.s))
  }
  </code></pre>
  
  <p>When I use underscores as the docs indicate (eg, substitute <code>r._i</code> for <code>r.i</code> above) I get the following compile error:</p>
  
  <blockquote>
    <p><code>r._i undefined (type C.struct_rec has no field or method _i)</code></p>
  </blockquote>
  
  <p>When I don't use underscores it works fine. I tried this with both pointers and non-pointers. The only other idea I can think of is that maybe it's because I allocated the instances in GO rather than C, is that the case??</p>
  
  <p>Thanks for any help!</p>