Curriculum Interfaces & generics Generics exercise 1 · mcq
Generics
Translate the TypeScript generic identity function to Go. Generics landed in Go 1.18 and look almost like TS — type parameters in SQUARE BRACKETS, before the value parameters.
TypeScript reference
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.