title: How to merge two Go values of same struct type?
body: <p>I want to create a function called <code>merge()</code> that takes in two values of the same struct, but of <em>any</em> struct, and returns the merged values of the two structs. </p>
  
  <p>I want the first value to take precedence. For example, if there are two structs <code>a</code> and <code>b</code>, after calling <code>merge(a,b)</code>, if there are fields that both <code>a</code> and <code>b</code> contain, I want it to have <code>a</code>'s value for that given field. </p>
  
  <p>What would be the best way to implement this? 
  <a href="https://play.golang.org/p/7s9PWx26gfz" rel="noreferrer">https://play.golang.org/p/7s9PWx26gfz</a></p>
  
  <pre><code>type cat struct {
  name  string
  color string
  age   int
  }
  
  type book struct {
  title  string
  author string
  }
  
  func main() {
  c1 := cat{
      name:  "Oscar",
      color: "",
      age:   3,
  }
  
  c2 := cat{
      name:  "",
      color: "orange",
      age:   2,
  }
  
  c3 := merge(c1, c2)
  
  // want: c3 = cat{
  //               name: "Oscar",
  //               color: "orange",
  //               age: 3,
  //       }
  
  
  
  // another case...
  b1 := book{
      title: "Lord of the Rings",
      author: "John Smith",
  }
  
  b2 := book{
      title: "Harry Potter",
      author: "",
  }
  
  b3 := merge(b1, b2)
  
  // want: b3 = book{
  //               title: "Lord of the Rings",
  //               author: "John Smith",
  //       }
  }
  </code></pre>
  
  <p>This is what I have so far:</p>
  
  <pre><code>// merges two structs, where a's values take precendence over b's values (a's values will be kept over b's if each field has a value)
  func merge(a, b interface{}) (*interface{}, error) {
      var result interface{}
      aFields := reflect.Fields(a)
      bFields := reflect.Fields(b)
  
      if !reflect.DeepEqual(aFields, bFields) {
          return &amp;result, errors.New("cannot merge structs of different struct types")
      }
  
      aValOf := reflect.ValueOf(a)
      bValOf := reflect.ValueOf(b)
      resultValOf := reflect.ValueOf(result)
      aValues := make([]interface{}, aValOf.NumField())
      resultValues := make([]interface{}, resultValOf.NumField())
  
      for i := 0; i &lt; aValOf.NumField(); i++ {
          if reflect.ValueOf(aValues[i]).IsNil() {
              resultValues[i] = bValOf.Field(i).Interface()
              break
          }
          resultValues[i] = aValOf.Field(i).Interface()
      }
      return &amp;result, nil
  }
  </code></pre>