Generics: added in Go 1.18, they let you write reusable code (like a generic Map function) parameterized by type, avoiding both code duplication and the loss of type safety that interface{} caused.
2 / 5
What is a type parameter in a generic Go function?
Type parameter: in func Max[T constraints.Ordered](a, b T) T, the T in brackets is a type parameter. The compiler infers or you supply the concrete type at the call site.
3 / 5
What is a constraint in Go generics?
Constraint: an interface defining the operations or set of types allowed for a type parameter. constraints.Ordered, for example, permits only types that support comparison operators like < and >.
4 / 5
How does Go decide the concrete type via type inference?
Type inference: often you need not specify the type explicitly; calling Max(3, 5) lets the compiler infer T = int from the arguments, keeping generic code concise.
5 / 5
What is the any constraint in Go generics?
any: the broadest constraint, an alias for the empty interface. A type parameter constrained by any accepts every type, useful for containers that do not need type-specific operations.