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)
| 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. |
| 115 | func 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. |