Curriculum Interfaces & generics Generics
Generics
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.
9 ready
01
pick oneTranslate the TypeScript generic identity function to Go.
02
pick one TypeScript lets you constrain a generic with extends:
03
pick oneTypeScript constrains a generic with a union of literal types
04
fill blanksType the constraint that allows ANY type as the type
05
fill blanks Type the constraint that requires T to support == and
06
type one lineWrite the function SIGNATURE for a generic `Max[T int |
07
type one lineInstantiate a generic STRUCT type: create a `Pair[string,
08
write a program Build a generic Stack[T any] with three methods, all
09
write a programTheme 4.2 capstone — extract a REUSABLE constraint interface