Skip to content

Instantly share code, notes, and snippets.

@falnyr
Last active May 11, 2019 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save falnyr/4643f4b4a8c20975d5ddb67055f0f675 to your computer and use it in GitHub Desktop.
Save falnyr/4643f4b4a8c20975d5ddb67055f0f675 to your computer and use it in GitHub Desktop.
go-cmp custom comparer issue
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