GO LEARNING PATH · 5 LESSONS
Generics, and when not to
Type parameters, constraints, and the times an interface is the better answer.
A type parameter is written in square brackets
Write a function that works for several types without interface{} and without a cast.
3 min · Worked lesson
A constraint is an interface with types in it
Constrain a type parameter to what your code actually needs.
3 min · Worked lesson
A type can take parameters too
Write a container that holds one type rather than any type.
3 min · Worked lesson
A method can take its own type parameter
Write the method that could only be a function before Go 1.27.
3 min · Worked lesson
Most code that could be generic should not be
Recognise the three cases where a type parameter earns its place, and the ones where it does not.
3 min · Prediction challenge