I am searching a []interface{}
slice for a given interface{}
value:
var v interface{}
for i := 0; i < len(A); i++ {
if (A[i] == v) {
fmt.Println("Gotcha!")
break
}
}
In the trivial case the types are int
. However what should I do if, for example, the types are some custom struct
?
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
:
You can also try this playground https://play.golang.org/p/bgO1_V87v9k
In other words, handling equality seems easy in Go!