When to use generics
Generics is not a panacea and cannot replace good, accurate, and rational program design. Therefore, here are some principles and personal suggestions to keep in mind when thinking about using generics to solve a problem:
- Generics might be used when creating code that needs to work with multiple data types.
- Generics should be used when an implementation with interfaces and reflection makes the code more complex and more difficult to understand than necessary.
- Moreover, generics might be used when you expect to support more data types in the future.
- Once again, the goal of using anything while coding is code simplicity and easier maintenance, not bragging about your coding capabilities.
- Lastly, generics can be used when the developer is feeling comfortable with generics. There is no Go rule that makes the use of generics mandatory.
This section concludes this chapter. Keep in mind that in order to use the cmp
, slices
...