Proposals Unofficial Go proposal tracker

Add generic Set with custom hashing and equality (experimental)

#80584 · x/exp/collection/hashed · low impact · Accepted ·

Go's built-in maps compare keys with ==, so a set built on a map cannot hold elements 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 Set[E] hash table in the experimental golang.org/x/exp/collection/hashed package, the set counterpart of hashed.Map. NewSet takes a maphash.Hasher[E], which supplies a hash function and an equality check for elements. The set offers Insert, Contains, Delete, and other set operations. It also provides the All iterator.

Set operations Union, Intersection, Difference, and SymmetricDifference return a new set, and their ...With variants update the receiver; Intersects reports whether two sets share an element. Elements must not be (or contain) NaN floats.

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)
}

s := hashed.NewSet[types.Type](typeHasher{})
s.Insert(t1)
s.Insert(t2) // false if t2 is identical to t1
fmt.Println(s.Contains(t1), s.Len())
for t := range s.All() {
    fmt.Println(t)
}

For elements with the usual == equality, set.Set remains the right choice.

Proposal
#80584 by adonovan
Status
Accepted on Nov 4, 2026
Impact
low
Area
x/exp/collection/hashed