Go Generics: When to Use Them, When to Avoid Them
Go generics fill a specific blind spot: algorithms that work identically for any type. Real-world experience on patterns that work and those that make code worse.
Tag archive
Go generics fill a specific blind spot: algorithms that work identically for any type. Real-world experience on patterns that work and those that make code worse.

Generics beat interfaces for some Go code and add noise to the rest. Type sets, real container and numeric examples, and the compile-time cost.

Five infer recipes past the handbook: tuple last/head/tail, URL param extraction, overload-aware constructors, promise-chain resolution.

Go's maps stdlib package: 8 functions that delete handwritten helpers. Keys/Values/Clone/Copy/DeleteFunc/Equal + the iter.Seq trap.

any, comparable, cmp.Ordered, and the tilde. Four type-set constructs that decide whether your generic Go code compiles, and what it forgets.
Java added generics in Java 5, and the team made a call that still echoes today: type erasure. At...

It's 2026. You've watched the language wars get louder for another year. Rust keeps eating systems...
Bilingual post · Post bilíngue Jump to: English · Português English {#english} ...

Generics in Go look free at the call site. The compiler bills you for them later in time go build and binary size. Three patterns that cut the bill.

200+ uses of any in one Go codebase, three patterns under the hood. Generics, small consumer-side interfaces, and typed structs replaced almost all of it.

Four years in, Go generics have a clear shipping list. Type inference, comparable, Result/Option. And a list of things that still don't work.

You shipped generics in 2022 and still write any inside them. Three patterns where the escape hatch is hiding, and what the constraint should be.