Creating common interfaces in Golang
If your API can have multiple implementations, follow this process:
- Try to write a vanilla Go interface that supports all implementations.
- Do not use
interface{}
types — #todo consider generics for this - If your interface uses a custom type, extract it into a separate package.
- Do not use
- If you cannot come up with a clean interface, don’t do it. Instead, introduce an educational pattern for everyone to follow, to write alternative implementations.
Do not perform Golang-gymnastics to achieve generic code. Such tricks end up with code that’s hard to follow.