Go Reference ยท Concepts
Generics rules in Go
The rules of Go generics: type parameter syntax, constraints, comparable and ~T, how type inference works, what is not allowed, and when to avoid generics.
Generics, added in Go 1.18, let one function or type work for many types while staying type-checked at compile time. A generic declaration lists type parameters in square brackets, and each parameter has a constraint, an interface that says which types are allowed and which operations the body may use.
The rules are intentionally small. This page covers how constraints work, how the compiler infers type arguments, and the limits you will meet: no generic methods, no type switches on type parameters without conversion to any, and no operations the constraint does not guarantee.
func Map[T, U any](xs []T, f func(T) U) []U { /* ... */ } type Stack[T any] struct{ items []T }
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.
The rules
- Constraints are interfaces.
anyis "no restriction" (and no operations except assignment and passing around). A union such asint | stringlimits the type set.~Tmeans "any type whose underlying type is T". - Operations must be allowed by the whole type set.
+needs every member to support it;<needs an ordered type (cmp.Ordered);==needscomparable. comparableincludes types that can be compared with==. Interface types satisfy it since Go 1.20, though comparison may panic at run time if the dynamic types are not comparable.- Inference works from function arguments, including function-typed arguments and assignments to typed function variables (Go 1.21). It does not infer from return types alone, so
Zero[string]()needs the argument. Untyped constants default toint,float64, and so on; with mixed constant kinds Go 1.21 picks the "largest" kind. - Methods cannot declare their own type parameters. A method may use only the receiver's parameters. Use a generic function instead.
- Generic type aliases are supported from Go 1.24.
- No specialization, no default type arguments, no variadic type parameters, no type parameters on a bare
const. - A type parameter cannot be embedded in a struct, and a type parameter cannot be used as its own constraint.
- Core types: operations like
range, indexing andmakework when all types in the set share the same underlying shape (for example only slices of one element type).
Usage notes
- Do not reach for generics too early. Write the concrete code first and generalize when you repeat it for a second or third type.
- Interfaces are still better when the behavior differs per type (
io.Reader,fmt.Stringer). Generics fit when the algorithm is the same and the type varies: containers,slices,maps,iterhelpers. - Go compiles generics with GC shape stenciling: types with the same shape share code, with a dictionary passed for type-specific operations. Pointer-shaped types share one instantiation; a generic call is usually close to a normal call, but can be slower than hand-written code in tight loops.
- Error messages: "T does not satisfy comparable" and "cannot use generic function without instantiation" are the common ones.
- The standard library's
slices,maps,cmpandsync/atomic.Pointer[T]are good examples of idiomatic generic APIs.
Related
interface,type,map, Range-over-func iterators- Lessons: Generics, Constraints, Generic types