title: How do I initialize a composed struct in Go?
body: <p>Let's say I have a struct with another struct embedded in it.</p>
  
  <pre><code>type Base struct {
      ID string
  }
  
  type Child struct {
      Base
      a int
      b int
  }
  </code></pre>
  
  <p>When I go to initialize <code>Child</code>, I can't initialize the <code>ID</code> field directly.</p>
  
  <pre><code>// unknown field 'ID' in struct literal of type Child
  child := Child{ ID: id, a: a, b: b }
  </code></pre>
  
  <p>I instead have to initialize the ID field separately.</p>
  
  <pre><code>child := Child{ a: 23, b: 42 }
  child.ID = "foo"
  </code></pre>
  
  <p>This would seem to violate encapsulation. The user of Child has to know there's something different about the ID field. If I later moved a public field into an embedded struct, that might break initialization.</p>
  
  <p>I could write a <code>NewFoo()</code> method for every struct and hope everyone uses that, but <strong><em>is there a way to use the struct literal safely with embedded structs that doesn't reveal some of the fields are embedded?</em></strong> Or am I applying the wrong pattern here?</p>