title: golang - reflection on embedded structs
body: <p>Given a struct like so:</p>
  
  <pre><code>type B struct {
      X string
      Y string
  }
  
  type D struct {
      B
      Z string
  }
  </code></pre>
  
  <p>I want to reflect on D and get to the fields X, Y, Z. </p>
  
  <p>Intuitively, before attempting the solution, I was assuming I would be able to traverse the struct D and get all fields using reflection (X, Y, Z) and won't have to deal with B. </p>
  
  <p>But as you can see, I only see the embedded struct B using reflection and not its fields.</p>
  
  <p><a href="http://play.golang.org/p/qZQD5GdTA8" rel="noreferrer">http://play.golang.org/p/qZQD5GdTA8</a></p>
  
  <p>Is there a way I can make B fully transparent when reflecting on D? </p>
  
  <p>Why do I want this? </p>
  
  <p>Imaging a common struct (B in the example here), that is used in multiple other structs by using embedding. Using reflection, the attempt is to copy D into another similar struct in a different package. The destination struct for copying will have all attributes flatly laid out (no embedding there). So there is a mismatch from the source to the destination (embedding vs no embedding) but all the attributes flatly laid out are the same. I don't want to create custom solutions for each struct. </p>