Three new books, Go Optimizations 101, Go Details & Tips 101 and Go Generics 101 are published now.

About Go Custom Generics

The main purpose of custom generics is to avoid code repetitions, or in other words, to increase code reusability.

For some situations, generics could also lead to cleaner code and simpler APIs (not always).

For some situations, generics could also improve code execution performance (again not always).

Before version 1.18, for many Go programmers, the lack of custom generics caused pains in Go programming under some situations.

Indeed, the pains caused by the lack of custom generics were alleviated to a certain extend by the following facts.

However, the pains are still there for many use cases. The demand for custom generics became stronger and stronger. In the end, the Go core team decided to support custom generics in Go.

For all sorts of reasons, including considerations of syntax/semantics backward compatibility and implementation difficulties, the Go core team settled down on the type parameters proposal to implement custom generics.

The first Go version supporting custom generics is 1.18 (the current latest version).

The type parameters proposal tries to solve many code reuse problems, but not all. And please note that, not all the features mentioned in the parameters proposal have been implemented yet currently (Go toolchain version 1.18). The custom generics design and implementation will continue to evolve and get improved in future Go versions. And please note that the proposal is not the sky of Go custom generics.

Despite the restrictions (temporary or permanent ones) in the current Go custom generics design and implementation, I also have found there are some details which are handled gracefully and beautifully in the implementation.

Although Go custom generics couldn't solve all code reuse problems, personally, I believe Go custom generics will be used widely in future Go programming.


Index↡

The Go 101 project is hosted on Github. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.

If you would like to learn some Go details and facts every serveral days, please follow Go 101's official Twitter account @go100and1 or join Go 101 slack channels.

The digital versions of this book are available at the following places:
Tapir, the author of Go 101, has been on writing the Go 101 series books and maintaining the go101.org website since 2016 July. New contents will be continually added to the book and the website from time to time. Tapir is also an indie game developer. You can also support Go 101 by playing Tapir's games (made for both Android and iPhone/iPad):
Individual donations via PayPal are also welcome.

Index: