Introduction

Let’s get an overview of Go generics.

We'll cover the following

Overview of generics

This chapter is about generics and how to use the new syntax to write generic functions and define generic data types. The new generics syntax became available with the release of Go 1.18.

Let us make something clear from the beginning: we do not have to use Go generics if we do not want to, and we can still write wonderful, efficient, maintainable, and correct software in Go! Additionally, the fact that we can use generics and support lots of data types, if not all available data types, does not mean that we should do that. Always support the required data types, no more, no less, but do not forget to keep an eye on the future of our data and the possibility of supporting data types that were not known at the time of writing our code.

Topics to cover

This chapter covers:

  • Introducing generics

  • Constraints

  • Defining new data types with generics

  • Interfaces vs. generics

  • Reflection vs. generics

  • Concluding remarks: what does the future look like for Go developers?

Get hands-on with 1200+ tech skills courses.