Go version: go1.21rc2
I'm writing a function using generics in Go, that takes a value and returns true/false if the value is in the map.
In version 1 below, I don't understand why the two values can't be compared when they're of type any
? But after casting to type any
in version 2, it now works... I assume I've missed something obvious but I don't really understand this.
Version 1 (NOT WORKING):
func InValues[M ~map[K]V, K comparable, V any](m M, v V) bool {
for _, x := range maps.Values(m) {
if x == v {
return true
}
}
return false
}
Version 2 (WORKING):
func InValues[M ~map[K]V, K comparable, V any](m M, v V) bool {
for _, x := range maps.Values(m) {
if any(x) == any(v) {
return true
}
}
return false
}