title: Comparing struct fields using reflect
body: <p>I am trying to do updates on structs for use in a PUT API. I need to find out if a field in the new struct has a different value as the same field in the old struct. I have never used reflect before, so I am a bit confused. Here is the code I have, I expected it to only print fields that are different, but it prints every field.</p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "reflect"
  )
  
  type Permission struct {
      User       int    `json:"user" db:"user"`
      ObjectId   int    `json:"object_id" db:"object_id"`
      ObjectType string `json:"object_type" db:"object_type"`
      Permission string `json:"codename" db:"codename"`
  }
  
  func main() {
      old := Permission{1, 1, "site", "view_site"}
      new := Permission{1, 2, "site", "edit_site"}
      v1 := reflect.ValueOf(old)
      v2 := reflect.ValueOf(new)
      t := reflect.TypeOf(old)
      for i := 0; i &lt; v1.NumField(); i++ {
          if v2.Field(i) != v1.Field(i) {
              fmt.Printf("%v ", t.Field(i).Name)
              fmt.Printf("old: %v ", v1.Field(i))
              fmt.Printf("new: %v ", v2.Field(i))
              fmt.Println("")
          }
      }
  }
  </code></pre>
  
  <p>I guess the reason for this is that each <code>Value</code> is a different struct and so they are not equal, but I cannot seem to figure out how to actually do what I need to do.</p>