Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Built-in Types

Go constraints and the ~T approximation element

Reference for Go constraint interfaces: type sets, union elements, the ~T approximation operator, cmp.Ordered, embedding and core-type rules.

A constraint is an interface that describes the set of types a type parameter may take. Beyond method lists, a constraint interface can contain type elements: a single type (int), a union (int | float64), or an approximation ~int, which means "int and every type whose underlying type is int".

The set of types an interface allows is its type set. Inside a generic function you may use exactly the operations that all types in the type set support.

Go
type Number interface {
	~int | ~int64 | ~float64
}

Try it

Go Unions and ~T

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.

Go Mixing methods and type sets

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

Go
interface{ int | string }          // union of exact types
interface{ ~int }                  // underlying type int
interface{ ~[]E }                  // used with a second type parameter E
interface{ comparable; Name() string }
Element Meaning
T Exactly the type T.
~T All types whose underlying type is T. T itself must be its own underlying type (a non-defined type).
A | B Union of the type sets.
Several lines Intersection: a type must satisfy all of them.
Methods Listed alongside type elements; types must have those methods.

Errors: an interface with type elements can only be used as a constraint, not as a variable type (var x Number fails). The ~ form cannot be applied to interface types or to a type whose underlying type differs from the operand.

Usage notes

  • Why ~? Without it, type Celsius float64 would not satisfy float64. Use ~ for numeric constraints so named types are accepted.
  • Operations come from the whole type set. + works if every type in the set supports it; < only if all are ordered. cmp.Ordered (Go 1.21) packages all ordered types: integers, floats and strings.
  • Core types. Operations like range, indexing and make need the type set to share a single underlying (core) type, which is why S ~[]E is the typical way to accept any slice type.
  • Unions of interfaces with methods are not allowed; a union can contain only non-interface types or interfaces without methods.
  • Do not constrain too tightly. A constraint listing int | float64 rejects int32; the standard golang.org/x/exp/constraints package (now mostly replaced by cmp.Ordered) offers Integer, Float and Signed families.
  • Inline constraints are allowed: [T int | string] instead of naming an interface.
  • Compile-time only. Type sets are erased from run-time behavior; they affect what compiles, not what executes.