title: How to marshal struct as if it were an anonymous struct?
body: <p>The <a href="https://golang.org/pkg/encoding/json/#Marshal" rel="nofollow noreferrer">documentation</a> states:</p>
  <blockquote>
  <p>Anonymous struct fields are usually marshaled as if their inner exported fields were fields in the outer struct.</p>
  </blockquote>
  <p>For examble:</p>
  <pre><code>type foo struct {
      Foo1 string `json:&quot;foo1&quot;`
      Foo2 string `json:&quot;foo2&quot;`
  }
  
  type boo struct {
      Boo1 string `json:&quot;boo1&quot;`
      foo
  }
  </code></pre>
  <p>and I do this:</p>
  <pre><code>s := boo{
      Boo: &quot;boo1&quot;,
      foo: foo{
          Foo1: &quot;foo1&quot;,
          Foo2: &quot;foo2&quot;,
      },
  }
  
  b, err := json.MarshalIndent(s, &quot;&quot;, &quot;   &quot;)
  fmt.Println(string(b))
  </code></pre>
  <p>I get this:</p>
  <pre><code>{
      &quot;boo1&quot;: &quot;boo1&quot;,
      &quot;foo1&quot;: &quot;foo1&quot;,
      &quot;foo2&quot;: &quot;foo2&quot;
  }
  </code></pre>
  <p>But how can I achieve the same result when the foo is <em>Not</em> an anonymous struct? Meaning:</p>
  <pre><code>type boo struct {
      Boo string `json:&quot;boo&quot;`
      Foo foo
  }
  </code></pre>
  <p>And also unmarshaling the json.</p>