Do slices and maps have compatible type sets regarding the range operator?
I was trying to iterate through either, where slice keys are the indexes, such that this would work:
type KV[K comparable, V any] interface {
~[]V | map[K]V
}
func f[KV2 KV[K, V], K comparable, V any](kv KV2) {
for k, v := range kv { // line 12
fmt.Println(k, v)
}
}
func main() {
f[map[string]string, string, string](map[string]string{"a": "b", "c": "d"}) // works
f[[]string, int, string]([]string{"e", "f"}) // error
}
I get this error:
./prog.go:12:20: cannot range over kv (variable of type KV2 constrained by KV[K, V]) (KV2 has no core type)
I'm having trouble understanding this error message. Is this saying that maps and slices don't have a core type because they're not compatible for the range operator?