go get github.com/slashdevops/comparatorUpdate to the latest available version:
go get -u github.com/slashdevops/comparatorcomparator requires Go 1.26 or newer and has zero third-party
dependencies — only the Go standard library.
package main
import (
"fmt"
"github.com/slashdevops/comparator"
)
func main() {
a := map[string]int{"cpu": 2, "mem": 8}
b := map[string]int{"cpu": 2, "mem": 8}
if comparator.Equal(a, b) {
fmt.Println("values are equal")
}
}comparator.Equal is a package-level convenience function. It builds a
comparator, runs a single comparison, and returns a bool. For repeated
comparisons with the same configuration, create a comparator once (see below).
Returned by New and
NewWithOptions. Use it for equality checks:
comp := comparator.New()
comp.Equal(a, b) // bool
comp.Diff(a, b) // ([]Difference, error)
comp.CompareWithDiff(a, b) // *DiffResultReturned by NewDiffComparer. It extends Comparator with the
richer diff and formatting operations:
comp := comparator.NewDiffComparer()
comp.CompareWithDiff(a, b) // *DiffResult
comp.GetUnifiedDiff(a, b) // (*UnifiedDiff, error)
comp.GetJSONPatch(a, b) // ([]JSONPatchOperation, error)
comp.GetVisualDiff(a, b) // (*VisualDiff, error)
comp.FormatDiff(result, format) // (string, error)When you only need a single call, skip the constructor entirely:
| Function | Returns | Use for |
|---|---|---|
Equal(a, b, opts...) |
bool |
One-off equality check. |
DeepEqual(a, b, opts...) |
bool |
Same as Equal; familiar name for reflect.DeepEqual users. |
CompareWithDiff(a, b, opts...) |
*DiffResult |
One-off detailed diff. |
GetJSONPatch(a, b, opts...) |
([]JSONPatchOperation, error) |
One-off RFC 6902 patch. |
All of them accept the same functional options as the constructors.
The comparator handles every Go type:
- Primitives:
bool,int*,uint*,float*,complex*,string - Composite: arrays, slices, maps, structs
- Reference: pointers, interfaces, channels, functions
- Special:
time.Time(compared with its nativeEqualmethod)
Recursive structures are handled safely via cycle detection.
- Tune behavior with Configuration & Options.
- Understand diff output in Diffing.
- Present diffs with Output Formats.