Skip to content

Instantly share code, notes, and snippets.

@niko-dunixi
Created March 15, 2024 16:51
Show Gist options
  • Save niko-dunixi/be372b63cef97d92ae3570678ceb6c1f to your computer and use it in GitHub Desktop.
Save niko-dunixi/be372b63cef97d92ae3570678ceb6c1f to your computer and use it in GitHub Desktop.
Find unequal struct fields in golang
// Custom comparison function
func compareStructs(a, b interface{}) {
valA := reflect.ValueOf(a)
valB := reflect.ValueOf(b)
for i := 0; i < valA.NumField(); i++ {
fieldA := valA.Field(i)
fieldB := valB.Field(i)
if !reflect.DeepEqual(fieldA.Interface(), fieldB.Interface()) {
fmt.Printf("Field %s is not equal: %v != %v\n", valA.Type().Field(i).Name, fieldA.Interface(), fieldB.Interface())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment