Checking equality of interface{}

reflect.DeepEqual(x, y any) bool

worked.


I have a summary of properties of Go types

properties of Go types

Explanation

  • depends - it's only allowed if the contained type(s) are comparable.) For Interface types the code will compile but if at runtime the types contained are not comparable then the runtime will panic. Thanks to @Andrew W. Phillips.

Thanks to @CodingPickle comment, I provide the following from the Go Programming Language Specification

The equality operators == and != apply to operands that are comparable.

Regarding interface{}s and structs:

  • Interface values are comparable. Two interface values are equal if they have identical dynamic types and equal dynamic values or if both have value nil.
  • A value x of non-interface type X and a value t of interface type T are comparable when values of type X are comparable and X implements T. They are equal if t's dynamic type is identical to X and t's dynamic value is equal to x.
  • Struct values are comparable if all their fields are comparable. Two struct values are equal if their corresponding non-blank fields are equal.

You can also try this playground https://play.golang.org/p/bgO1_V87v9k

In other words, handling equality seems easy in Go!


I am correcting my previous answer as I now believe it was wrong. When two interface values are compared the run-time will only panic if they are of the same type and it is non-comparable. If they contain different types then the result is false even if one or both types are non-comparable.

What are non-comparable types? Basically, they are slices, maps, functions and any struct or array type that uses them.