Add generic Map with custom hashing and equality (experimental)
Go's built-in maps compare keys with ==, so they cannot handle keys whose equality is defined some other way. For example, two types.Type values can be identical according to types.Identical but still differ under ==.
The proposal adds a generic Map[K, V] hash table in the experimental golang.org/x/exp/collection/hashed package. NewMap takes a maphash.Hasher[K], which supplies a hash function and an equality check for keys. Equal keys must produce equal hashes. The map offers Set, Get, Lookup, Contains, and other map operations. It also provides the All, Keys, and Values iterators.
type typeHasher struct{}
func (typeHasher) Hash(h *maphash.Hash, t types.Type) {
// hash t consistently with Identical
}
func (typeHasher) Equal(x, y types.Type) bool {
return types.Identical(x, y)
}
m := hashed.NewMap[types.Type, string](typeHasher{})
m.Set(t, "info")
fmt.Println(m.Get(t), m.Len())
for k, v := range m.All() {
fmt.Println(k, v)
}
For keys with the usual == equality, the built-in map remains the right choice.
- Proposal
- #69559 by adonovan
- Status
- Accepted on Nov 4, 2026
- Impact
- low
- Area
- x/exp/collection/hashed