Generics in Go - A language design perspective

By Mohit Kumar

Elevator Pitch

This talk is different from other talks on Go generics. In this talk, I’ll cover how generics work internally and what happens at the compiler level. I’ll show code samples to simplify these concepts. This knowledge will be helpful if you’re writing libraries and frameworks in Go.

Description

1. Why Generics?

We’ll start the talk by understanding why Generics exists in programming languages. We’ll discuss its role in writing reusable, type-safe code and explain this with practical examples.

2. Go Before Generics

We’ll explain with code examples how developers used Reflection before Generics and how it lacked the compile time safety that Generics provides.

3. Language Mechanisms Supporting Generics

The heart of our talk will be a deep dive into the language mechanisms that support generics in Go. We’ll explain the following concepts with examples:

  • Type parameter list: We’ll discuss how these are similar to the ordinary parameter list but for types.
  • Interface Type: We’ll see how and why interfaces were extended in Go to enable generics.
  • Type Inference: We’ll touch upon the different types of inferences that occur in Go and how they contribute to the flexibility and power of generics.
  • Type Sets: We’ll discuss the concept of type sets, a new meta-type addition in Go.

4. Best Practices and Gotchas

Finally, we’ll end our discussion by exploring the best practices and gotchas with generics. We’ll learn how to effectively use generics to write efficient, type-safe code and what pitfalls to avoid.

Notes

This talk is suitable for people with an intermediate level of knowledge in Go. This talk is technical, but we’ll include code examples to simplify the concepts.

Here are the links to the talk presented at a recent Go meetup.

Slides - https://docs.google.com/presentation/d/1QLyHTZff-9bMvzc4gyfhGp5x51O2riK4/edit

Talk recording - https://drive.google.com/drive/u/0/folders/1391epBbX04w4ISYtj_2eWTfW-A7Em1vd (I’ll condense this talk to 20-25 mins and only cover most important concepts)