aka generic guidelines
It's late 2021, and Go is about to add "generics" (i.e., type parameters for types and functions) to upcoming release version 1.18.
Here is some guidance for Go programmers, cribbed from a talk by Ian Lance Taylor.
In other words, you might not need generics.
When it comes to generics, if you start writing your program by defining new type parameter constraints, you're probably on the wrong path.
Start by writing functions. It's easy to add type parameters later, when it's clear that they'll be useful.
- Functions that work on slices, maps, and channels of any element type
- General purpose data structures (eg., linked list, binary tree)
- When operating on type parameters, prefer functions to methods
- When a method looks the same for all types
- When just calling a method on the type argument (use an interface type)
- When the implementation of a common method is different for each type
- When the operation is different for each type, even without a method (use reflection)
Avoid boilerplate: if you find yourself writing the exact same code multiple times, where the only difference between the copies is that the code uses different types, consider whether you can use a type parameter.
Corollary: don't use type parameters prematurely, wait until you are about to write boilerplate code.