Writing generic code in Go
The introduction of generics in Go was a highly debated and anticipated feature. Some developers felt that introducing it would contradict Go’s core principles of simplicity, while others felt like it was a sign of maturity and would allow them to write better production code. As with every technical solution or design decision, there is a trade-off between the advantages and disadvantages.
As previously mentioned, generics refers to the ability to write code that works with different data types without being limited to a specific type. In the absence of generics, we have used Go interfaces to implement generic behavior in Go. In Chapter 3, Mocking and Assertion Frameworks, we explored the power of interfaces and have seen how they can be used for wrapping and replacing dependencies. While interfaces are not the same as generics, they provide a way to achieve similar goals of flexibility and code reuse.
Figure 11.1 provides a comparison of generics...