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

Function ExampleOption_avoidEqualMethod

cmp/example_test.go:229–250  ·  cmp/example_test.go::ExampleOption_avoidEqualMethod

If the Equal method defined on a type is not suitable, the type can be dynamically transformed to be stripped of the Equal method (or any method for that matter).

()

Source from the content-addressed store, hash-verified

227// dynamically transformed to be stripped of the Equal method (or any method
228// for that matter).
229func ExampleOption_avoidEqualMethod() {
230 // Suppose otherString.Equal performs a case-insensitive equality,
231 // which is too loose for our needs.
232 // We can avoid the methods of otherString by declaring a new type.
233 type myString otherString
234
235 // This transformer converts otherString to myString, allowing Equal to use
236 // other Options to determine equality.
237 trans := cmp.Transformer("", func(in otherString) myString {
238 return myString(in)
239 })
240
241 x := []otherString{"foo", "bar", "baz"}
242 y := []otherString{"fOO", "bAr", "Baz"} // Same as before, but with different case
243
244 fmt.Println(cmp.Equal(x, y)) // Equal because of case-insensitivity
245 fmt.Println(cmp.Equal(x, y, trans)) // Not equal because of more exact equality
246
247 // Output:
248 // true
249 // false
250}
251
252func roundF64(z float64) float64 {
253 if z < 0 {

Callers

nothing calls this directly

Calls 3

TransformerFunction · 0.92
EqualFunction · 0.92
myStringTypeAlias · 0.85

Tested by

no test coverage detected