Last active
May 11, 2019 01:21
-
-
Save falnyr/4643f4b4a8c20975d5ddb67055f0f675 to your computer and use it in GitHub Desktop.
go-cmp custom comparer issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/google/go-cmp/cmp" | |
"time" | |
) | |
type MyTime time.Time | |
type Foo struct { | |
Bar string | |
Created MyTime | |
} | |
func main() { | |
a := Foo{ | |
Bar: "this is a", | |
Created: MyTime(time.Now()), | |
} | |
b := Foo{ | |
Bar: "this is b", | |
Created: MyTime(time.Now()), | |
} | |
comparer := cmp.Comparer(func(a, b Foo) bool { | |
return time.Time(a.Created).Equal(time.Time(b.Created)) | |
}) | |
if !cmp.Equal(a, b, comparer) { | |
fmt.Printf("not equal: %s", cmp.Diff(a, b)) | |
} else { | |
fmt.Printf("all good") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment