Radar · Go · Archive · Week 28 · Jul 7 – 13, 2025
Generic interfaces blog post
Nice to knowlanguage
- What changed
- The blog explains that adding type parameters to interface types is surprisingly powerful, highlighting new patterns and use cases.
- Production impact
- The source does not say.
- Try it
- Read the blog and experiment with a generic interface in a small program.
- Source
- go.dev/blog/generic-interfaces
Understand it, then run it
The Go blog shows that interfaces can now have type parameters. Before this, an interface was just a set of method signatures. With type parameters, an interface can describe a whole family of interfaces, one for each type argument. This lets you write generic code that requires a type to implement a method that takes its own type, such as a Compare method for ordering.
Run it now
// This program demonstrates a generic interface with a type parameter.
// It compiles with Go 1.27.1, which already supports generic interfaces.
package main
import (
"fmt"
"time"
)
// Comparer is a generic interface that requires a Compare method
// accepting a value of the same type.
type Comparer[T any] interface {
Compare(T) int
}
// TimeWrapper wraps time.Time to implement Comparer[TimeWrapper].
type TimeWrapper struct{ t time.Time }
// Compare compares two TimeWrapper values using the underlying time.Time.
func (tw TimeWrapper) Compare(other TimeWrapper) int {
if tw.t.Before(other.t) {
return -1
}
if tw.t.After(other.t) {
return 1
}
return 0
}
// Max returns the larger of two values that satisfy Comparer.
func Max[T Comparer[T]](a, b T) T {
if a.Compare(b) >= 0 {
return a
}
return b
}
func main() {
now := TimeWrapper{t: time.Now()}
earlier := TimeWrapper{t: time.Now().Add(-time.Hour)}
// Max uses the generic Comparer interface to pick the later time.
later := Max(now, earlier)
fmt.Println("Later time:", later.t)
}
What it printed when we ran it on Go 1.27.1
Later time: 2026-10-01 08:46:22.227620274 +0000 UTC m=+0.000043182
Run sends this program (for Solidity, the contract and its tests) to our own sandbox, where it is compiled and run once, with no network, and what it printed or the test report comes back here. Nothing is kept. Runs are counted per visitor for the day so everyone gets a turn; the details are on the legal page.
Written by gpt-oss-20b from the linked source · claims checked against the sources · archive, not individually reviewed