Golang proposal: container/: generic collection types
The Go Collections working group has proposed several new collection APIs for Go 1.28, including hash-based maps and sets, ordered maps, and a generic binary heap API. These additions aim to bring common collection data structures to the standard library, guided by the fa…
Intelligence analysis by Llama
The Go Collections working group has proposed several new collection APIs for Go 1.28, including hash-based maps and sets, ordered maps, and a generic binary heap API. These additions aim to bring common collection data structures to the standard library, guided by the familiar Go principles of pragmatism and simplicity.
Imagine you have a big box of toys, and you want to find a specific toy. You can use a map to keep track of where each toy is. But what if you have a lot of toys, and you want to find a specific one quickly? That's where the new collection APIs come in. They help you organize your toys in a way that makes it easy to find what you need.
Analysis
A $60B Vote of Confidence
The Go Collections working group has proposed several new collection APIs for Go 1.28, including hash-based maps and sets, ordered maps, and a generic binary heap API. These additions aim to bring common collection data structures to the standard library, guided by the familiar Go principles of pragmatism and simplicity.
The proposed additions include hash/maphash.Hasher, a standard interface for expressing custom hash functions and equivalence relations for arbitrary data types. This is useful when the key type is not comparable, such as a slice or map. The package docs include an example of its use in a Bloom filter.
Another proposed addition is container/hash.Map[K,V], a hash-based Map that uses the custom hash functions mentioned above. This is a more convenient and transparent way to represent sets whose elements are comparable.
The group has also proposed container/ordered.Map[K,V], an ordered mapping. The current implementation uses a balanced binary tree, but nothing in the design requires that. The common Go pattern of building a map[K]V then sorting its keys performs well in most cases, but on occasion, such as when a range query is needed, other data structures perform much better.
Why Cursor?
The proposed additions will bring common collection data structures to the standard library, making it easier for developers to work with collections in Go. This will improve the overall usability and maintainability of Go code. The new packages will live in the existing container tree, and the group prefers the term “collection” to avoid confusion with the container virtualization concept from Linux.
The Road Ahead
The initial implementations of all the proposed data structures aim to satisfy the API and asymptotic performance expectations as simply as possible. There are doubtless many opportunities for later optimizations to reduce constant factors, but they are out of scope of the proposal process. The group expects to consider additional proposals in due course, such as insertion-ordered hash maps and stacks.
Key points
- The Go Collections working group has proposed several new collection APIs for Go 1.28.
- The proposed additions include hash-based maps and sets, ordered maps, and a generic binary heap API.
- These additions aim to bring common collection data structures to the standard library, guided by the familiar Go principles of pragmatism and simplicity.
- The new packages will live in the existing container tree, and the group prefers the term “collection” to avoid confusion with the container virtualization concept from Linux.
The proposed additions will make it easier for developers to work with collections in Go, which will improve the overall usability and maintainability of Go code. This will lead to more efficient and effective development, and will ultimately benefit the Go community as a whole.
The proposed additions may not be widely adopted, which could limit their impact. Additionally, the complexity of the new APIs may make them difficult for some developers to understand and use.