Archive · Week 25 · Jun 16 – 22, 2025from 2 items
This week the Go team accepted two proposals that add new APIs to the standard library. One introduces a generic hashing interface in the `maphash` package, and the other adds a helper method for working with process handles in the `os` package.
Worth knowingstdlib
hash/maphash: standardize the hash function
- 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
Explain it
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.
Worth knowingstdlib
os: add (*Process).WithHandle
- What changed
- A new method
WithHandleonos.Processcalls a supplied function with a valid process handle (pidfd on Linux 5.4+ or a Windows handle). If no handle is available it returnsErrNoHandle. - Production impact
- The source does not say.
- Try it
- Create an
os.Processand callWithHandleto print the handle value. - Source
- github.com/golang/go/issues/70352
Exercise
Write a small program that creates a child process, then uses (*os.Process).WithHandle to print the process handle. The program should compile and run on Linux 5.4+ or Windows; on other systems it should print the error returned by ErrNoHandle.
The 60-second version
Hello, this week the Go team added two new helpers to the standard library. First, in the `maphash` package, they introduced a generic `Hasher` interface and a ready‑made `ComparableHasher` for comparable types. This lets you write type‑specific hash functions that are guaranteed to be stateless and consistent with equality. Second, the `os` package now offers a `WithHandle` method on `os.Process`. It gives you a safe way to access the underlying process handle—whether that’s a pidfd on recent Linux kernels or a Windows handle—by calling a callback while the handle is guaranteed to be valid. These additions give developers more fine‑grained control over hashing and process interaction without breaking existing code.
Written by gpt-oss-20b · claims checked against the sources · archive, not individually reviewed