Compare Go values with cmp
masterThe cmp package provides a powerful and safer alternative to reflect.DeepEqual for determining if two values are semantically equal.
Key capabilities include:
- Custom Equality: Use custom equality functions to override default behavior (e.g., comparing floats within a specific tolerance).
- Method-based Equality: If a type defines an
Equalmethod,cmpwill use it to determine equality. - Recursive Comparison: If no custom options or
Equalmethods are present, it recursively compares primitive kinds. - Unexported Field Safety: Unlike
reflect.DeepEqual,cmpdoes not compare unexported fields by default. Attempting to compare them will cause a panic unless you explicitly handle them usingcmpopts.IgnoreUnexportedor theAllowUnexportedoption.