title: Reflect thinks struct Value is also a ptr?
body: <p>I have a data structure like this demo. As you can see, <code>foo</code> has an embedded pointer to <code>bar</code>:</p>
  
  <pre><code>type foo struct {
      *bar
  }
  
  type bar struct {
      S []byte
  }
  </code></pre>
  
  <p>And I'm using the <code>reflect</code> package like this:</p>
  
  <pre><code>func test(x interface{}) {
  
      var v = reflect.ValueOf(x)
  
      if v.Kind() == reflect.Struct {
          fmt.Println("was a struct")
  
      // panic: reflect: call of reflect.Value.Elem on struct Value
      //  v = v.Elem()
  
      // panic: reflect: call of reflect.Value.Field on ptr Value
          v = v.FieldByName("S")
      }
  }
  
  func main() {
      var f foo
      test(f)
      fmt.Println(string(f.S))
  }
  </code></pre>
  
  <p>So <code>v.Kind()</code> is recognized as a <code>reflect.Struct</code>, but if I try to treat it like a struct by using <code>.FieldByName("S")</code>, it panics because it thinks <code>v</code> is a <code>ptr</code>.</p>
  
  <p>So then if I try to treat it like a <code>ptr</code> by calling <code>.Elem()</code>, it panics because it thinks <code>v</code> is a <code>struct</code>.</p>
  
  <p>I've tried <code>reflect.Indirect()</code>, as well as a few other things, but I can't figure out how to get the field of an embedded pointer. </p>
  
  <p>Is there a way to get the <code>reflect.Value</code> representation from an embedded pointer to a struct?</p>
  
  <p><em><strong>Demo:</em></strong> <a href="http://play.golang.org/p/n0eea6XW3I" rel="nofollow">http://play.golang.org/p/n0eea6XW3I</a></p>
  
  <hr>
  
  <p><em><strong>EDIT:</em></strong> Also tried <code>v = v.FieldByName("bar")</code>, but got: </p>
  
  <blockquote>
    <p><code>panic: runtime error: invalid memory address or nil pointer dereference</code></p>
  </blockquote>