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).
()
| 227 | // dynamically transformed to be stripped of the Equal method (or any method |
| 228 | // for that matter). |
| 229 | func 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 | |
| 252 | func roundF64(z float64) float64 { |
| 253 | if z < 0 { |
nothing calls this directly
no test coverage detected