Curriculum Interfaces & generics Generics exercise 2 · mcq
Generics
TypeScript lets you constrain a generic with extends:
``ts
function index<T extends Equatable>(...) { ... }
`
Go uses a different model — and it has a built-in constraint
for "anything you can == against" that's load-bearing because
it's the bare-minimum requirement for using a type as a map key
or comparing values with ==.
Which Go built-in constraint covers "any type that supports
== / !=`"?
About this theme
Mostly familiar from TS. func Foo[T any](x T) T { ... }. The constraint syntax is where Go adds something new: an interface used as a constraint can list types or use ~T for "any type whose underlying type is T". The comparable constraint is built-in. Method sets in constraints are powerful and worth slowing down on.