In Go, a type constraint is an interface that limits which types may be used as a type argument for a generic function or type. It can require methods, list allowed types with a type set such as ~int | ~float64, or both. "Type bounds" is the general name from other languages for the same idea: Go has no separate feature, the constraint is the bound.
Constraints let the compiler check that the operations you use on a type parameter, such as <, + or a method call, are valid for every permitted type. Generics arrived in Go 1.18, and cmp.Ordered joined the standard library in Go 1.21.
A type parameter list gives every parameter a constraint. The constraint any means no restriction, but then you can do very little with the value.
Any non-trivial operation requires a tighter constraint, because the function body must be valid for every type in the constraint.
A constraint can require methods, exactly like a normal interface.
The standard library already has fmt.Stringer, so you would normally use that instead of redefining it.
To use operators, list the types that support them. A union inside an interface defines a type set, and the tilde ~T means "any type whose underlying type is T".
Without the tilde, Celsius would not satisfy the constraint, because its type is not literally float64. Interfaces that contain a type set can only be used as constraints, never as the type of a variable.
Two predeclared or standard-library constraints cover the most common needs.
| Constraint | Provided by | Allows |
|---|---|---|
any | Language | Every type, no operations |
comparable | Language | == and !=; usable as map keys |
cmp.Ordered | cmp package (Go 1.21) | <, <=, >, >= on ints, floats and strings |
The older golang.org/x/exp/constraints package is no longer needed for ordering; use cmp.Ordered. Since Go 1.20, comparable is also satisfied by interface types, though comparing them can panic at run time if the dynamic values are not comparable.
A constraint can require both a type set and methods, which is useful for numeric types that also know how to print themselves.
Constraints can also refer to other type parameters, for example func Map[S ~[]E, E, R any](s S, f func(E) R) []R, which accepts a named slice type and returns a plain slice.
type ID int.any, do not require Number.In Go they are the same thing. A constraint is the interface after the type parameter that bounds the permitted type arguments.
~int includes int and every defined type with underlying type int.
Define an interface with a union of numeric types, such as ~int | ~float64, and use it as the constraint. Reuse one definition across your code.
Yes. cmp.Ordered in the standard cmp package covers all ordered types, including strings.
Type constraints are interfaces that bound type parameters. Use method requirements for behavior, type sets with tildes for operators, and the predeclared comparable and cmp.Ordered for equality and ordering. Choose the tightest constraint the function body needs and no tighter.