title: How to initialize private fields that are default nil when making the zero value useful?
body: <p>I am trying to follow the Go proverb &quot;Make the zero value useful&quot; but run into an issue with private fields on my struct that are by default <code>nil</code> (like <code>map</code>). If the zero value of my struct is to be useful, at some point it needs to initialize the private field. Consider the following situation in which I would like to do away with the constructor for <code>Response</code>, and make the zero value of <code>Response</code> useful. Obviously the field <code>bag</code> (which is a <code>map[string]interface{}</code>) needs to be initialized. I try to initialize the field <code>bag</code> when the getter <code>Bag()</code> is called on Response, which works, and allows for then calling <code>Set()</code> on the bag to set a value. But somehow when the response ends up back in the main function, my value gets lost.
  I believe the problem is that the initialization that I came up with replaces the pointer on a copy of Response, instead of changing what the pointer points at. So the old pointer still points to <code>nil</code>, but I am not sure. How to initialize the private field correctly without the constructor?</p>
  <pre><code>func main() {
      response := getResponse()
      number := response.Bag().Get(&quot;NiceNumber&quot;)
      fmt.Println(response.Name, &quot;tells me that your nice number today is:&quot;, number)
  }
  
  func getResponse() (response Response) {
      response = newResponse() // I am trying to do away with this line.
      response.Name = &quot;MyResponse&quot;
      response.Bag().Set(&quot;NiceNumber&quot;, 100)
      return
  }
  
  type Response struct {
      Name string
      bag  Bag
  }
  
  func (r Response) Bag() Bag {
      // I have added this piece of code as an attempt to initialize r.bag
      if r.bag == nil {
          r.bag = Bag(make(map[string]interface{}))
      }
      // ---
      return r.bag
  }
  
  type Bag map[string]interface{}
  
  func (b Bag) Set(name string, value interface{}) {
      b[name] = value
  }
  
  func (b Bag) Get(name string) interface{} {
      return b[name]
  }
  
  // This constructor would be deleted if possible.
  func newResponse() Response {
      return Response{
          Name: &quot;&quot;,
          bag:  Bag(make(map[string]interface{})),
      }
  }
  </code></pre>
  <p>Output now is <code>MyResponse tells me that your nice number today is: 100</code>, and when I remove the call to the constructor of Response, the output is <code>MyResponse tells me that your nice number today is: &lt;nil&gt;</code>.</p>