从键类型的语言规范:

必须为键类型的操作数完全定义比较运算符 == 和 ! = ;

所以大多数类型都可以用作键类型,但是:

切片、映射和函数值不可比较

因此不能用作映射键。

anyinterface{}

type mytype struct{}

type ss []string


_ = make(map[interface{}]interface{}) // this works...

_ = make(map[any]any)                 // ... semantically the same

_ = make(map[mytype]any)              // even a struct


_ = make(map[ss]any) // FAILS: invalid map key type ss

https://go.dev/play/p/OX_utGp8nfH