Is it possible to define equality for named types/structs?

golang compare structs
golang array equality
golang map equality
golang diff structs
golang interface equality
golang struct as map key
golang compare two ints
golang compare types

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.


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

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.

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.

Structure Equality in Golang, concept of struct equality. // using == operator. package main. import "fmt". // Creating a structure. type Author struct {. name string. branch string. You can base your definition of value equality on a comparison of all the fields and properties in the type, or you can base the definition on a subset. In either case, and in both classes and structs, your implementation should follow the five guarantees of equivalence (For the following rules, assume that x, y and z are not null): x.Equals(x) returns true. This is called the reflexive property. x.Equals(y) returns the same value as y.Equals(x). This is called the symmetric property.


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.

C# Journey into struct equality comparison, deep dive, Deep dive on struct equality comparison, stepping into . var ann = new Person { Age = 22, Name = "Ann" };var isSamePerson = john == ann; Basically, in order to be able to compare struct with == operator you must explicitly implement it. So, Equals for class types uses referential comparison, but for� For most other types, testing for value equality is more complex because it requires that you understand how the type defines it. For classes and structs that have multiple fields or properties, value equality is often defined to mean that all fields or properties have the same value.


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.

Type Equality in LLVM, It went wrong in many ways. 'Types with the same name are the same type' (false ). This is a very simple heuristic: %struct. In this case, where object_names are specified, the type name (product) becomes optional: struct requires either a type_name or at least one name in object_names, but not necessarily both. It is important to clearly differentiate between what is the structure type name ( product ), and what is an object of this type ( apple , banana , and melon ).


How to define value equality for a type, You can base your definition of value equality on a comparison of all the fields and properties In either case, and in both classes and structs, your implementation should follow the five This is called the symmetric property. A struct in the C programming language (and many derivatives) is a composite data type (or record) declaration that defines a physically grouped list of variables under one name in a block of memory, allowing the different variables to be accessed via a single pointer or by the struct declared name which returns the same address.


Surprising struct equality test - Usage, struct T name::String end T("hello") == T("hello") I have to define hashing in this simple example (my type is literally this How can I fix this? First, let's define our term. Merriam-Webster defines the word "equality" as "the quality, fact, or state of being equal." Clearly, for a complete definition of "equality," we also need to define the word "equal." Merriam-Webster's first two definitions of the adjective form of the word seem to provide the most relevant answer for our purposes


Create, initialize and compare structs � YourBasic Go, Struct types; 2 ways to create and initialize a new struct; Compare structs To define a new struct type, you list the names and types of each field. The default� The good news is that you don’t actually have to implement any custom logic to support reference equality comparisons in your types. This functionality is provided for all types by the static ReferenceEquals () method as well as by the Equals () method. The latter actually calls the former when it is invoked.