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.



本丛书微信公众号(联系方式一)名称为"Go 101"。二维码在网站首页。此公众号将时不时地发表一些Go语言相关的原创短文。各位如果感兴趣,可以搜索关注一下。

《Go语言101》系列丛书项目目前托管在Github上(联系方式二)。欢迎各位在此项目中通过提交bug和PR的方式来改进完善《Go语言101》丛书中的各篇文章。我们可以在项目目录下运行go run .来浏览和确认各种改动。



The English version of this book is here.