Go Generics in Production: Patterns That Actually Work
Go 1.18 shipped generics in March 2022. I have been using them in production code since mid-2022, and after a year of real-world use, I have developed a clear picture of where generics genuinely improve code and where they add accidental complexity. This post is the practical guide I wish I had when generics landed. What Problem Do Generics Solve? Before Go 1.18, code that needed to work with multiple types had three options: use interface{} and type-assert at runtime (losing compile-time safety), use code generation (maintenance burden), or copy-paste (duplication). None of these are good. Generics provide a fourth option: parametrised functions and types that work across concrete types while preserving compile-time type safety. The canonical examples — a typed Stack, a Map function over slices — are often dismissed as toy problems. But real production code has the same patterns: generic result types, typed caches, repository abstractions, event buses. Let me show you the patte...