Can anyone recommend a good package or method of comparing two values of the same struct type?
For example, I have struct 1 and struct 2, both of the same type. I want to compare them to see if the values in the fields in both structs are the same (maybe even just specific fields in each struct). If they’re the same, great, but if not, what fields have different values and how can I then make them the same?
Thanks in advance for your help!
Hello, take a look at reflect package, you can iterate struct’s fields, get types and check values.
Often, you can just check if they’re equal and then if not, assign them: https://play.golang.org/p/PRbSAlCdHsC
This only works if none of the struct’s fields (or fields of any structs embedded within the struct you’re comparing) are maps, slices, or chans. If they are, then try
reflect.DeepEqual instead of just
Make sure you dereference pointers or else you’re copying the pointer and not the struct fields: https://play.golang.org/p/tt6PFOc3cLW
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.