title: "Merge" fields two structs of same type
body: <p>Looking at this <code>struct</code>:</p>
  
  <pre><code>type Config struct {
    path string
    id   string
    key  string
    addr string
    size uint64
  }
  </code></pre>
  
  <p>Now I have a <code>DefaultConfig</code> intialized with some values and one loaded from a file, let's say <code>FileConfig</code>.
  I want both structs to me merged, so that I get a <code>Config</code> with the content of both structs. <code>FileConfig</code> should override anything set in <code>DefaultConfig</code>, while <code>FileConfig</code> <strong>may not have all fields set</strong>.
  (Why this? Because a potential user may not know the default value, so removing that entry would be equivalent to setting the default - I think)</p>
  
  <p>I thought I'd need reflection for this:</p>
  
  <pre><code> func merge(default *Config, file *Config) (*Config) {
    b := reflect.ValueOf(default).Elem()
    o := reflect.ValueOf(file).Elem()
  
    for i := 0; i &lt; b.NumField(); i++ {
      defaultField := b.Field(i)
      fileField := o.Field(i)
      if defaultField.Interface() != reflect.Zero(fileField.Type()).Interface() {
       defaultField.Set(reflect.ValueOf(fileField.Interface()))
      }
    }
  
    return default
   }
  </code></pre>
  
  <p>Here I am not sure:</p>
  
  <ul>
  <li>If reflection is needed at all</li>
  <li>There may be easier ways to do this</li>
  </ul>
  
  <p>Another issue I see here is that checking for zero values may be tricky: what if the overriding struct <em>intends</em> to override with a zero value? Luckily, I don't think it applies to my case - but this becomes a function, it may become a problem later</p>