liveBook · Manning

Comparable

comparable is Go’s predeclared generic constraint satisfied by types whose values can be compared with == or !=.

How 3 Manning books explain it

Learn Go with Pocket-Sized Projects

comparable is Go's predeclared generic constraint satisfied by types whose values can be compared with == or !=.

How it is used
Constrains generic map and cache keys so equality-based lookup is valid.
How deep it goes
Defines equality support, enumerates comparable and non-comparable types, and applies the constraint in a generic cache.
Examples
Uses integer cache keys, string values, and a map[K]V declaration.
What you can do after
Build it from scratch by declaring generic map-backed types with equality-valid key parameters.

Read how Learn Go with Pocket-Sized Projects explains comparable →

Go in Action, Second Edition

comparable is a predeclared Go interface constraint satisfied by types whose values support equality and inequality comparisons.

How it is used
Constrains generic collection elements when operations need equality comparisons.
How deep it goes
Explains the constraint through a linked-list declaration, constructor, and methods that compare elements.
Examples
Uses a generic linked list with Contains and Remove methods, plus an integer-list instantiation.
What you can do after
Builds a generic collection whose element constraint supports equality-based searches and removals.

Read how Go in Action, Second Edition explains comparable →

100 Go Mistakes and How to Avoid Them

Comparable is a Go predeclared constraint satisfied by types whose values can be compared with == or !=.

How it is used
Constrains generic map keys to types supporting equality while leaving map values unrestricted.
How deep it goes
Explains constraint selection and traces both compile-time comparison errors and runtime interface comparison panics.
Examples
Uses a generic getKeys function and customer structs with slice fields to demonstrate valid keys and failed comparisons.
What you can do after
Apply it in Go generic map functions to constrain key types.

Read how 100 Go Mistakes and How to Avoid Them explains comparable →

What to know first

Related concepts

Questions about comparable

What is comparable?

comparable is Go’s predeclared generic constraint satisfied by types whose values can be compared with == or !=.

Which Manning books explain comparable?

Learn Go with Pocket-Sized Projects, Go in Action, Second Edition and 100 Go Mistakes and How to Avoid Them.

What should I know before learning comparable?

Manning books that teach comparable usually introduce boolean, type safety and struct first.

What is comparable a kind of?

Comparable is a kind of type constraint. A type constraint is a condition on a type or type parameter that restricts valid types or specifies properties and operations they must support.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.