Two slices may be considered equal if they have the same elements, regardless of the order that they appear in. Transformations can be used to sort the slice. This example is for demonstrative purposes; use [github.com/google/go-cmp/cmp/cmpopts.SortSlices] instead.
()
| 196 | // This example is for demonstrative purposes; |
| 197 | // use [github.com/google/go-cmp/cmp/cmpopts.SortSlices] instead. |
| 198 | func ExampleOption_sortedSlice() { |
| 199 | // This Transformer sorts a []int. |
| 200 | trans := cmp.Transformer("Sort", func(in []int) []int { |
| 201 | out := append([]int(nil), in...) // Copy input to avoid mutating it |
| 202 | sort.Ints(out) |
| 203 | return out |
| 204 | }) |
| 205 | |
| 206 | x := struct{ Ints []int }{[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}} |
| 207 | y := struct{ Ints []int }{[]int{2, 8, 0, 9, 6, 1, 4, 7, 3, 5}} |
| 208 | z := struct{ Ints []int }{[]int{0, 0, 1, 2, 3, 4, 5, 6, 7, 8}} |
| 209 | |
| 210 | fmt.Println(cmp.Equal(x, y, trans)) |
| 211 | fmt.Println(cmp.Equal(y, z, trans)) |
| 212 | fmt.Println(cmp.Equal(z, x, trans)) |
| 213 | |
| 214 | // Output: |
| 215 | // true |
| 216 | // false |
| 217 | // false |
| 218 | } |
| 219 | |
| 220 | type otherString string |
| 221 |
nothing calls this directly
no test coverage detected