0009: Generics Complement Interfaces, Not Replace Them
Date: 2026-06-14 Status: Accepted
Insight
Go generics (1.18+) solve a specific problem: writing functions and data structures that work with any type without sacrificing type safety. Before generics, you had two options: interface{} (loses type safety) or code generation (verbose). Now you can write func Max[T constraints.Ordered](a, b T) T.
The key teaching point: generics do NOT replace interfaces. They solve different problems. Interfaces are for abstracting behavior (Lesson 0001). Generics are for abstracting types in containers and algorithms. This distinction is crucial because many developers coming from Java/C++ assume generics replace interfaces — they don't.
Cloud-native Go uses generics sparingly. Kubernetes added generic Set[T] and Queue[T] types. Prometheus uses generics for metric registration helpers. But most production code still uses interfaces. The lesson should teach both when to use generics AND when not to.
Consequences
- Lesson 0009: Type parameters, constraints, generic functions/types, type inference, the
constraintspackage, when to use generics vs interfaces - Should explicitly reference Lesson 0001 (interfaces) and explain the complementarity
- Exercises: implement a generic Set, a generic Min/Max function, understand constraint satisfaction