title: JSON and struct composition in Go
body: <p>I have hierarchical structure similar to this <code>Network -&gt; Serie -&gt; Season -&gt; Episodes</code>. This is a simplified version my real hierarchy is 7 levels deep.</p>
  
  <p>I need to decode/encode these objects with the following JSON:</p>
  
  <pre><code>Network:
  {
    id: 1,
    name: 'Fox'
  }
  
  Series:
  {
     id: 2,
     name: 'The Simpsons',
     network: {
        id: 1,
        name: 'Fox'
     }
  }
  
  Season:
  {
     id: 3,
     name: 'Season 3',
     network: {
        id: 1,
        name: 'Fox'
  
     },
     series: {
        id: 2,
        name: 'The Simpsons'
     }
  }
  
  Episode:
  {
     id: 4,
     name: 'Pilot',
     network: {
        id: 1,
        name: 'Fox'
     },
     series: {
        id: 2,
        name: 'The Simpsons'
     },
     season: {
        id: 3,
        name: 'Season 3'
     }
  }
  </code></pre>
  
  <p>I tried composing my objects like these:</p>
  
  <pre><code>type Base struct {
    ID   string `json:"id"`
    Name string `json:"name"`
  }
  
  type Network struct {
    Base
  }
  
  type Series struct {
    Network // Flatten out all Network properties (ID + Name)
    Network Base `json:"network"`
  }
  
  type Season struct {
    Series  // Flatten out all Series properties (ID + Name + Network)
    Series Base `json:"series"`
  }
  
  type Episode struct {
    Season  // Flatten out all Season properties (ID + Name + Network + Series)
    Season Season `json:"season"`
  }
  </code></pre>
  
  <p>Of course it doesn't work due <code>"duplicate field error"</code>. In addition the JSON result would be deeply nested.
  In classic OOP this is fairly easy using normal inheritance, in prototype languages it's also doable (Object.create/Mixins)</p>
  
  <p>Is there an elegant way of do this with golang? I would prefer to keep code DRY.</p>