Sometimes, an empty map or slice is considered equal to an allocated one of zero length. This example is for demonstrative purposes; use [github.com/google/go-cmp/cmp/cmpopts.EquateEmpty] instead.
()
| 161 | // This example is for demonstrative purposes; |
| 162 | // use [github.com/google/go-cmp/cmp/cmpopts.EquateEmpty] instead. |
| 163 | func ExampleOption_equalEmpty() { |
| 164 | alwaysEqual := cmp.Comparer(func(_, _ interface{}) bool { return true }) |
| 165 | |
| 166 | // This option handles slices and maps of any type. |
| 167 | opt := cmp.FilterValues(func(x, y interface{}) bool { |
| 168 | vx, vy := reflect.ValueOf(x), reflect.ValueOf(y) |
| 169 | return (vx.IsValid() && vy.IsValid() && vx.Type() == vy.Type()) && |
| 170 | (vx.Kind() == reflect.Slice || vx.Kind() == reflect.Map) && |
| 171 | (vx.Len() == 0 && vy.Len() == 0) |
| 172 | }, alwaysEqual) |
| 173 | |
| 174 | type S struct { |
| 175 | A []int |
| 176 | B map[string]bool |
| 177 | } |
| 178 | x := S{nil, make(map[string]bool, 100)} |
| 179 | y := S{make([]int, 0, 200), nil} |
| 180 | z := S{[]int{0}, nil} // []int has a single element (i.e., not empty) |
| 181 | |
| 182 | fmt.Println(cmp.Equal(x, y, opt)) |
| 183 | fmt.Println(cmp.Equal(y, z, opt)) |
| 184 | fmt.Println(cmp.Equal(z, x, opt)) |
| 185 | |
| 186 | // Output: |
| 187 | // true |
| 188 | // false |
| 189 | // false |
| 190 | } |
| 191 | |
| 192 | // Two slices may be considered equal if they have the same elements, |
| 193 | // regardless of the order that they appear in. Transformations can be used |
nothing calls this directly
no test coverage detected