Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
G

go-generics-the-hard-way

> 编程语言
Open source

A hands-on approach to getting started with Go generics.

1.9K stars0 likes0 views
WebsiteGitHub

About

A hands-on approach to getting started with Go generics.

Go generics the hard way

I started using Go back around 2015 and was immediately surprised by the lack of a generic type system. Sure, the empty interface{} existed, but that was hardly the same. At first I thought I wanted needed generics in Go, but over time I began appreciating the simplicity of the language. Therefore I was ambivalent at best when I learned of discussions to introduce generics in Go 2.0, and once the timetable was accelerated to 1.18, I decided it was time to dig into the proposal.

After a while, I gained an appreciation for how generics are implemented with the same elegance as Golang itself, and this moved me to share my experience. Go generics the hard way is a culmination of the time I spent playing with this new feature and provides a hands-on approach to learning all about generics in Go.

  • Labs: a hands-on approach to learning Go generics
  • FAQ: answers to some of the most frequently asked questions regarding Go generics
  • Links: links to related reference material and projects that use generics

Labs

  1. Prerequisites: how to install the prerequisites required to run the examples in this repository
  2. Hello world: a simple example using generics
  3. Getting started: an introduction to go generics
  4. Getting going: basic concepts explored
  5. Internals: how generics are implemented in golang
  6. Benchmarks: basic benchmarks for common patterns using generics
  7. Lessons learned: lessons learned from digging into generics

FAQ

  • How are you using generics in the Go playground?
  • What is T?
  • What is this any I keep seeing everywhere?
  • What does the tilde ~ do?
  • Do Go generics use type erasure?

How are you using generics in the Go playground?

We can use the Go playground in “Go dev branch” mode to edit and run your program with generics.

What is T?

The symbol T is often used when discussing generic types because T is the first letter of the word type. That is really all there is too it. Just like x or i are often the go-to variable names for loops, T is the go-to symbol for generic types.

For what is worth, K is often used when there is more than one generic type, ex. T, K.

What is this any I keep seeing everywhere?

The word any is a new, predeclared identifier and is equivalent to the empty interface in all ways. Simply put, writing and reading any is just more user friendly than interface{} :smiley:.

What does the tilde ~ do?

The ~ symbol is used to express that T may be satisfied by a defined or named type directly or by a type definition that has the same, underlying type as another defined or named type. To learn more about type constraints and the ~ symbol, please refer to the section Tilde ~.

Do Go generics use type erasure?

Generics in Go are not implemented with type erasure. Please jump to Internals for more information.

Links

Additional reading

  • Type parameter proposal: the accepted proposal for introdicing generics to go
  • Getting started with generics: a tutorial from the authors of go for getting started with generics
  • Go language specification: the reference manual for the go language
  • Go FAQ: frequently asked questions for go

Projects using generics

  • Controller-runtime: a write-up and patchset for implementing conditions logic, patch helpers, and simple reconcilers using generics
  • Go collections: generic utility functions for dealing with collections in go
  • go-generics-example: examples using generics

GitHub Issues· 0 open

View all on GitHub

No open issues yet, or sync has not completed.

Highlights

  • •Labs: a hands-on approach to learning Go generics
  • •FAQ: answers to some of the most frequently asked questions regarding Go generics
  • •Links: links to related reference material and projects that use generics
  • •How are you using generics in the Go playground?
  • •What is T?
  • •What is this any I keep seeing everywhere?
  • •What does the tilde ~ do?
  • •Do Go generics use _type erasure_?
  • •Type parameter proposal: the accepted proposal for introdicing generics to go
  • •Getting started with generics: a tutorial from the authors of go for getting started with generics

> Tags

Go

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言