Questions tagged [go-cmp]

go-cmp is a Go library for comparing values. It is typically used in unit tests. Use this tag together with [go] tag.

go-cmp is a library for comparing values. Due to performance trade-offs, it is best suited for non-production code, like unit tests. go-cmp can make it easier to computing diffs and compare values that aren't directly comparable with == and != as per the Go spec.

Useful links:

Basic example:

package main

import (
    "fmt"

    "github.com/google/go-cmp/cmp"
)

type Foo struct {
    Name   string
    Values []int
}

func main() {
    a := Foo{"Foo1", []int{1, 2, 3}}
    b := Foo{"Foo1", []int{1, 2, 3}}
    c := Foo{"Foo1", []int{1, 2, 5}}

    fmt.Println("is a equal to b?", cmp.Equal(a, b)) // true
    fmt.Println("is b equal to c?", cmp.Equal(b, c)) // false
}
7 questions
5
votes
1 answer

properly comparing and finding difference between two structs with exported members using go-cmp

I'm writing a Go application, and I want to create a test for it, in that test, I query something from the db, insert it into a struct, and compare that struct values with a static struct of the same type that I had, if they match, the test…
ufk
  • 30,912
  • 70
  • 235
  • 386
4
votes
1 answer

cmp.Equal gives panic message "cannot handle unexported field at .."

I have a custom struct which has an unexported field. I want to be able to compare values with type of this struct, but I am receiving panic message like this: panic: cannot handle unexported field at ... consider using a custom Comparer; if you…
buraky
  • 937
  • 1
  • 11
  • 18
3
votes
3 answers

How can I determine struct equality based on fields and not struct types in Golang?

I am currently using the go-cmp package to compare struct equality. For testing purposes I have the need to compare two different types of structs that should have the same fields with the same values. As a minimal example I am running into the…
Loupi
  • 550
  • 6
  • 14
2
votes
2 answers

comparing nested slices while ignoring order

In a test function, there is a case where nested slices should be compared. Say I have two varibles like the following: want := [][]string{{"bat"},{"nat","tan"},{"ate","eat","tea"}} got := [][]string{{"eat","tea","ate"},{"tan","nat"},{"bat"}} How…
Aliml
  • 41
  • 1
  • 4
2
votes
1 answer

Go cmp - how to define custom equality for a map with a struct as its keys?

Given a map that uses a struct as its key, where the values of the struct are pointers to another struct: type Dog struct { Name string } type Cat struct { Name string } type MapKey struct { dog *Dog cat *Cat } myMap :=…
Charlie Clarke
  • 177
  • 1
  • 9
2
votes
1 answer

Why does go-cmp Equal() say that the structs are not deeply equal, even though all fields are deeply equal?

I've been using reflect.DeepEqual to deeply compare structs using circular pointers. As that does not work with maps and for better test output, I've switched to go-cmp. Now I had to take note that even though cmp.Equal is supposed to be a drop-in…
keraldi
  • 53
  • 7
2
votes
2 answers

How to use go-сmp to compare such structs?

I have the following simplified set of structs type S0 struct { UUID uuid.UUID F00 int F01 int } type S1 struct { S0 F10 int F11 int } type S2 struct { S0 F20 int } <...> type SN struct { S0 FN0 int <...> FNM int } A…
JohnnyIpcom
  • 21
  • 1
  • 3