Add a generic Set type (experimental)
Go has no standard set type. Developers usually write map[T]struct{} or map[T]bool and reimplement operations like union and intersection by hand. This proposal adds a generic Set type in the experimental package golang.org/x/exp/collection/set. It gives the community a shared implementation to try before any set type reaches the standard library.
Set[E comparable] is defined as map[E]struct{}, so it works with range, len, and other map operations. Sets are created with Of (from values) or Collect (from an iterator). Methods cover insert, delete, membership checks, iteration, and set operations: union, intersection, difference, and symmetric difference. Each set operation has a version that returns a new set (Union) and one that updates the receiver (UnionWith).
x := set.Of(1, 2, 3)
y := set.Of(3, 4, 5)
fmt.Println(x.Union(y)) // {1, 2, 3, 4, 5}
fmt.Println(x.Intersection(y)) // {3}
x.Insert(10)
x.Delete(1)
fmt.Println(x.Contains(10), x.Len()) // true 3
// A Set is a map, so range works directly.
for elem := range y {
fmt.Println(elem)
}
- Proposal
- #69230 by lucas-deangelis
- Status
- Accepted on Nov 4, 2026
- Impact
- high
- Area
- x/exp/collection/set