Radar · Go · Archive · Week 25 · Jun 16 – 22, 2025
hash/maphash: standardize the hash function
Worth knowingstdlib
- What changed
- The proposal adds a generic
Hasher[T any]interface and aComparableHasher[T comparable]implementation to packagemaphash. - Production impact
- The source does not say.
- Try it
- Import
maphashand declare a type that implementsHasher[int]to see the new API. - Source
- github.com/golang/go/issues/70471
Understand it, then run it
The Go standard library has a package called hash/maphash that is used internally by maps to compute hash values. In older versions, the package did not expose a clear way for users to define their own hash functions for custom types. The change adds a generic interface called Hasher[T any] and a helper type ComparableHasher[T comparable]. Hasher describes two methods: Hash, which writes the hash of a value into a maphash.Hash, and Equal, which tells whether two values are considered the same for hashing purposes. ComparableHasher implements this interface for any type that already supports the == operator, so you can use it without writing extra code. With this change you can now write your own hash logic that works with Go’s map implementation, rather than relying on the default hashing of built‑in types.
Written by gpt-oss-20b from the linked source · claims checked against the sources · archive, not individually reviewed