50

After reading a related question about using slices in maps, I became curious about equality in Go.

I know it's possible to override the equals method of a Java Object. Is there a similar way to define how Go checks user defined types/structs for equality? If so, there would be a workaround for the issue referenced above. I thought using interface{} values might offer a solution but I received the error message panic: runtime error: hash of unhashable type []int.

Community
  • 1
  • 1
Bill DeRose
  • 2,330
  • 3
  • 25
  • 36
  • 3
    Not as far as I know. To work around you could convert objects to a hashable type for use in keys or even implement a hash-map from scratch using arrays. (Incidentally, some possibly-unexpected types _are_ hashable: pointers, and structs whose members are only hashable types.) – twotwotwo Dec 01 '13 at 08:05
  • 3
    Here's the list of "comparable" types: http://golang.org/ref/spec#Comparison_operators -- includes channels, surprisingly! – twotwotwo Dec 01 '13 at 08:22
  • 1
    If reading this in 2019 or later, the Equal function in github.com/google/go-cmp/cmp package looks like reflect.DeepEquals, but supports custom equality functions. – Chris Warth Aug 29 '19 at 03:37

3 Answers3

33

No. You can't modify the equality operator and there is no built-in way to add support for custom types to use == syntax. Instead you should compare the pointer values using reflect.DeepEqual.

Go supports equality checking structs.

type Person struct {
    Name string
}

a := Person{"Bill DeRose"}
b := Person{"Bill DeRose"}

a == b // true

It won't work with pointer fields (in the way you want) because the pointer addresses are different.

type Person struct {
    Friend *Person
}

a := Person{Friend: &Person{}}
b := Person{Friend: &Person{}}

a == b // false


import "reflect"

a := Person{Friend: &Person{}}
b := Person{Friend: &Person{}}

reflect.DeepEqual(a, b) // true

Keep in mind there are caveats.

In general DeepEqual is a recursive relaxation of Go's == operator. However, this idea is impossible to implement without some inconsistency. Specifically, it is possible for a value to be unequal to itself, either because it is of func type (uncomparable in general) or because it is a floating-point NaN value (not equal to itself in floating-point comparison), or because it is an array, struct, or interface containing such a value.

nz_21
  • 6,140
  • 7
  • 34
  • 80
AJcodez
  • 31,780
  • 20
  • 84
  • 118
  • 14
    This does not answer the question. – Bart Swennenhuis Oct 23 '19 at 11:31
  • 8
    Maybe the part "*You can't modify the equality operator and there is no built-in way to add support for custom types to use `==` syntax.*" should be marked in bold for people who read only the first sentence. – rustyx Oct 01 '20 at 12:31
  • 1
    Not only does this not answer the question, the suggestion to use `reflect.deepequal` is misleading and WILL NOT WORK in many simple situations (for example: when a struct field is a map where the keys are another struct). – shafeen Dec 23 '20 at 00:06
  • 1
    This answers the question in the first word! – William Pursell Nov 04 '22 at 13:23
27

No, this is not user-definable. Go has strict rules what counts as equal, and even what is comparable which itself is based on assignability. Take a look at the Comparison operators section of the spec.

twotwotwo
  • 28,310
  • 8
  • 69
  • 56
Volker
  • 40,468
  • 7
  • 81
  • 87
  • Is there an interface or method name convention for structs which have equivalence relations other than built-in equality? – kbolino Aug 09 '16 at 15:36
  • @kbolino I'm not aware of one. – Volker Aug 09 '16 at 17:57
  • 12
    The `net.IP` type has `func (ip IP) Equal(x IP) bool` which defines equivalence of IP addresses. Maybe that could be a model for such methods. – kbolino Aug 17 '16 at 02:02
11

There's no standard in Go language itself, yet (go 1.13).

However, comparison utilities could provide their own way to support it.

Function cmp.Equal (from google/go-cmp/cmp) supports definition of custom type comparator via definition of a Equal method:

• If the values have an Equal method of the form "(T) Equal(T) bool" or "(T) Equal(I) bool" where T is assignable to I, then use the result of x.Equal(y)even if x or y is nil. Otherwise, no such method exists and evaluation proceeds to the next rule.

kravemir
  • 10,636
  • 17
  • 64
  • 111