MCPcopy
hub / github.com/google/go-cmp / Diff

Function Diff

cmp/compare.go:115–137  ·  view source on GitHub ↗

Diff returns a human-readable report of the differences between two values: y - x. It returns an empty string if and only if Equal returns true for the same input values and options. The output is displayed as a literal in pseudo-Go syntax. At the start of each line, a "-" prefix indicates an eleme

(x, y interface{}, opts ...Option)

Source from the content-addressed store, hash-verified

113// Do not depend on this output being stable. If you need the ability to
114// programmatically interpret the difference, consider using a custom Reporter.
115func Diff(x, y interface{}, opts ...Option) string {
116 s := newState(opts)
117
118 // Optimization: If there are no other reporters, we can optimize for the
119 // common case where the result is equal (and thus no reported difference).
120 // This avoids the expensive construction of a difference tree.
121 if len(s.reporters) == 0 {
122 s.compareAny(rootStep(x, y))
123 if s.result.Equal() {
124 return ""
125 }
126 s.result = diff.Result{} // Reset results
127 }
128
129 r := new(defaultReporter)
130 s.reporters = append(s.reporters, reporter{r})
131 s.compareAny(rootStep(x, y))
132 d := r.String()
133 if (d == "") != s.result.Equal() {
134 panic("inconsistent difference and equality results")
135 }
136 return d
137}
138
139// rootStep constructs the first path step. If x and y have differing types,
140// then they are stored within an empty interface type.

Callers 5

TestDiffFunction · 0.92
BenchmarkBytesFunction · 0.92
ExampleDiff_testingFunction · 0.92
TestSortKeysFunction · 0.92

Calls 5

newStateFunction · 0.85
rootStepFunction · 0.85
compareAnyMethod · 0.80
EqualMethod · 0.65
StringMethod · 0.65

Tested by 5

TestDiffFunction · 0.74
BenchmarkBytesFunction · 0.74
ExampleDiff_testingFunction · 0.74
TestSortKeysFunction · 0.74