Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find which struct field is unequal when using reflect.DeepEqual

Tags:

go

How do I find which struct field is different when comparing two structs using reflect.DeepEqual(). When I print the struct values then I don't see any difference between the 2 struct values but still the reflect.DeepEqual() method returns false

like image 508
firstpostcommenter Avatar asked Jan 04 '23 04:01

firstpostcommenter


1 Answers

As pointed our by kostix in his (now deleted) answer https://github.com/go-test/deep is very useful. It helped me find my issues in minutes when comparing large structures based on XML/JSON documents.

diff := deep.Equal(got, want)
if diff != nil {
    t.Errorf("compare failed: %v", diff)
}

With the standard reflect package, I had to more or less implement the diff myself, but in this case it works and the diff shows all the field names up to the values which differ. It makes it really easy to find your issues.

like image 114
Alexis Wilke Avatar answered Jan 13 '23 08:01

Alexis Wilke