Use messagediff to compare Golang structs
masterThe messagediff library provides tools to perform deep comparisons between arbitrary Golang structs. It returns both a human-readable string describing the differences and a boolean indicating if the structs are equal.
Key features:
- Unexported Fields: If the
unsafepackage is available,messagediffwill diff unexported fields in addition to exported fields, which is useful for detailed testing error messages. - Field Ignoring: You can skip specific fields during comparison by adding the
`testdiff:"ignore"`struct tag.
package main
import "gopkg.in/d4l3k/messagediff.v1"
type someStruct struct {
A, b int
C []int
}
func main() {
a := someStruct{1, 2, []int{1}}
b := someStruct{1, 3, []int{1, 2}}
diff, equal := messagediff.PrettyDiff(a, b)
/*
diff =
`added: .C[1] = 2
modified: .b = 3`
equal = false
*/
}