Generic Go: Time for a Restart

Got comments?

A few months ago, I created a pre-order on Gumroad, for a book I had named "Generic Go: Get the Most Out Type Parameters and Avoid the Nasty Pitfalls."

Pre-order Generic Go

I wanted to write a guide about generic programming in Go without it becoming an ode to the recently accepted Type Parameters proposal. There are many ways to write generic Go code without compromising on its readability and I wanted to write a guide comparing them with one another.

For many months, I had lost my train of thought on that. Now I am slowly picking it up again.

Allow me to share a brief portion of the introduction with you, and let me know if that makes any sense.

What this Book Is About

Let's start by addressing the elephant in the room - generic type parameters. No other aspect (except error handling) has divided the Go community into two, so strongly opposing camps. Their addition to the language has been a requested and highly debated topic, ever since Go's first official release. Almost a decade, many draft proposals, and design documents later, it seems almost certain that they will see the light of day starting with Go 1.18. It is likely that by the time you are reading this book, this has already happened.

Let me get this straight - this book is not only about generic type parameters. In fact, only a portion of it really covers the accepted proposal. The reason for this is simple: there is more to generic programming than only type parameters. While I admit that they will help make certain things simpler, my biggest hope with this book is to show other ways of approaching our problems so that we use type parameters as sparingly as possible.

Like the introduction of modules in Go 1.13, generic type parameters are about to add a major change to the language - one that will need a couple of years before the Go community settles down on it. These first couple of years will witness the creation of new libraries, spurred by the enthusiasm to make everything generic. Much of this enthusiasm is about to fade away, as people discover by trial and error where it is appropriate to apply type parameters and where not.

My big goal with this book is to help the reader make an informed decision in the name of readable and maintainable Go code. We will cover several ways of writing generic code (interfaces, lambdas, reflection, code generation, type parameters) and point out where one is appropriate compared to the rest.