Constraints: comparable and interfaces
Union constraints and the comparable built-in
Constraints: comparable and interfaces is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is a constraint?
A constraint restricts which types can be used as a type argument. Without a constraint, the type parameter is any (the empty interface — only supports assignment and comparison via ==).
The any constraint
any (alias for interface{}) is the widest constraint — all types satisfy it. You can only use operations all types support: assignment and passing to interface parameters.
func Print[T any](v T) { fmt.Println(v) }The comparable constraint
comparable restricts the type parameter to types that support == and !=. Required for using the type as a map key or in equality checks.
func Contains[T comparable](s []T, v T) bool {
for _, item := range s {
if item == v { return true }
}
return false
}Interface constraints
Use an interface as a constraint to require specific methods. The type parameter must have all methods of the interface.
type Stringer interface { String() string }
func Print[T Stringer](v T) {
fmt.Println(v.String())
}Union constraints
Use a union of types with | to restrict to a set of concrete types. This enables using operators (like +) that are not part of any interface.
type Number interface {
int | int64 | float64
}
func Sum[T Number](s []T) T {
var total T
for _, v := range s { total += v }
return total
}~T for underlying types
Use ~T to include not just T but all types with T as their underlying type (e.g., custom types defined as type MyInt int).
type Integer interface { ~int | ~int64 }
type MyInt int
// MyInt satisfies Integer because its underlying type is intgolang.org/x/exp/constraints
The constraints package provides common constraint interfaces: Ordered (supports <), Integer, Float, Signed, Unsigned.
import "golang.org/x/exp/constraints"
func Min[T constraints.Ordered](a, b T) T {
if a < b { return a }
return b
}cmp package (Go 1.21)
Go 1.21 added cmp.Ordered constraint and cmp.Compare/cmp.Less functions in the standard library, replacing the exp/constraints dependency.
import "cmp"
func Min[T cmp.Ordered](a, b T) T { return min(a, b) }Combining constraints
Embed multiple interfaces in a constraint to require multiple properties:
type Printable interface {
comparable
fmt.Stringer
}Constraint inference
The compiler infers type parameters from arguments. If the constraint is too broad (any), explicit type arguments may be required to guide inference.
When to use comparable
Use comparable when the generic function needs to use == on values, store them in maps as keys, or use them as map/set elements. Not all types are comparable (slices, maps, funcs are not).
Quick Check
What does the ~ prefix mean in a union constraint like ~int?
Recap: Constraints
Key points:
- any: widest; comparable: supports ==; interface: requires methods
- Union (int|float64) enables arithmetic operators
- ~T includes named types with T as underlying type
- cmp.Ordered (Go 1.21+) for <, >, <=, >= constraints
Frequently asked questions
Is the “Constraints: comparable and interfaces” lesson free?
Yes — the full text of “Constraints: comparable and interfaces” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Constraints: comparable and interfaces”?
Union constraints and the comparable built-in You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Constraints: comparable and interfaces” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Type Parameters Syntax
- Constraints: comparable and interfaces
- Generic Data Structures
- Generics in Practice: Pitfalls