Go Reference ยท Built-in Types
Go type parameters (generics)
Reference for Go type parameters: generic functions and types, constraints, explicit instantiation, type inference, generic methods and current limits.
Type parameters, added in Go 1.18, let one function or type work with many types while staying statically checked. A type parameter list in square brackets follows the name: func Map[T, U any](...) or type Stack[T any] struct{...}. Each parameter has a constraint that says which types are allowed and which operations the body may use.
Callers usually do not write the type arguments: the compiler infers them from the ordinary arguments. Generics are best for containers and algorithms that do not depend on the element type, such as slices.Index or a Set[T].
func Max[T cmp.Ordered](a, b T) T { if a > b { return a } return b }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
func Name[P1 C1, P2 C2](args) result { ... } type Name[P1 C1, P2 C2] struct { ... } func (r *Name[P1, P2]) Method() { ... } // receiver repeats the parameters Name[int, string]{...} // explicit instantiation Name(a, b) // inferred
| Part | Description |
|---|---|
P |
A type parameter; behaves like a type name inside the declaration. |
C |
A constraint: an interface such as any, comparable, cmp.Ordered, or a union like int | float64. |
| Type arguments | May be partly given from the left (Convert[float64](x)) and the rest inferred. |
Errors: using an operator the constraint does not guarantee is a compile error; an argument that does not satisfy the constraint is a compile error naming the unmet requirement.
Usage notes
- Methods cannot have their own type parameters. Only the receiver's type parameters are available; use a generic function if you need more.
- Zero value: write
var zero Tto return "nothing" of typeT. - Inference works from function arguments, from assignment of a function value in Go 1.21+, and through constraint type inference (
S ~[]E). It does not use the result type for a plain call. - No specialization, no overloads. The same body is used for all types; you cannot write a different version for
int. - Generic type aliases (
type A[T any] = ...) are part of the language since Go 1.24. - Performance. Go uses "GC shape stenciling": types with the same memory shape share compiled code with a dictionary, so generics are not always as fast as hand-written code. Measure in hot loops.
- Do not overuse them. If a function only calls methods on its argument, an interface parameter is simpler.
- Type switches on a type parameter require converting to
anyfirst:switch any(v).(type).