Created
March 15, 2024 16:51
-
-
Save niko-dunixi/be372b63cef97d92ae3570678ceb6c1f to your computer and use it in GitHub Desktop.
Find unequal struct fields in golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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