What are Generics (Type Parameters) in Go?

Updated Apr 28, 2026

Short answer

Generics allow functions, types, and interfaces to operate on multiple data types while retaining compile-time type safety.

Deep explanation

Introduced in Go 1.18, you use square brackets [T any] to define type parameters. A type constraint (like any, comparable, or an interface) restricts the allowed types. The compiler generates specialized versions of the code for each type used (monomorphization) combined with GC dictionaries to balance binary size and speed.

Real-world example

Creating reusable, type-safe data structures like a generic Stack[T] or generic utility libraries for maps and slices.

Common mistakes

  • Overusing generics where simple interfaces would suffice, unnecessarily complicating function signatures and slowing down compile times.

Follow-up questions

  • What does the `comparable` constraint mean?

More Golang interview questions

View all →