• Stars
    star
    1,767
  • Rank 26,342 (Top 0.6 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 3 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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?

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{} ๐Ÿ˜ƒ.

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

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

More Repositories

1

memconn

MemConn is an in-memory network stack for Go.
Go
542
star
2

lem

a golang test framework for asserting expected escape analysis results & heap allocations
Go
56
star
3

go-interface-values

When storing a value in a Go interface allocates memory on the heap.
Go
42
star
4

gpd

Go Plug-ins & Vendored Dependencies
Go
32
star
5

gotil

Gotil provides many common, helpful utilities to other golang projects.
Go
22
star
6

gournal

Gournal (pronounced "Journal") is a Context-aware logging facade for Go.
Go
16
star
7

gpds

Go Plug-ins & Vendored Dependencies: A Solution
Go
11
star
8

yake2e

Turn-key solution for running Kubernetes e2e conformance tests on VMware vSphere
HCL
7
star
9

SubHead

This project serves as an example for how to update a submodule reference to the current submodule HEAD on a particular branch.
6
star
10

gofsutil

A Go library for filesystem related operations such as mount, format, etc.
Go
5
star
11

sortfold

A Go package for sorting string slices in increasing order using a case-insensitive comparison.
Go
5
star
12

gofig

Gofig simplifies external, runtime configuration of go programs.
Go
4
star
13

gotopt

Gotopt is a Go port of the C stdlib getopt library.
Go
4
star
14

goof

A drop-in replacement for the go "errors" package.
Go
3
star
15

gomk

A Makefile framework for golang projects.
Makefile
3
star
16

SubHeadSub

This project works in conjunction with the SubHeadSub project to serve as an example for how to update a submodule reference to the current submodule HEAD on a particular branch.
3
star
17

longboxapp

An open source, comic book and graphic novel organizational tool and reader for Mac OS X.
2
star
18

golf

A golang package for listing fields.
Go
2
star
19

nvn

nvn is a new approach to integrating .NET on Windows with the Maven build platform by driving builds with existing Microsoft build tools.
Java
2
star
20

gnixutils

Several common *NIX utilities ported to get going on portable go.
Go
2
star
21

pucr

Delve into the unexpected outcomes when patching/updating Kubernetes custom resources at different versions
Makefile
2
star
22

DedupeDictionary

A dictionary class for .NET (written in C#) that deduplicates value types so that many keys reference shared values.
2
star
23

kwik

Deploy Kubernetes quickly with an OVF
1
star
24

goioc

GoIoC provides a basic type registry for Go.
Go
1
star
25

gdj

Golang's "encoding/json" package with support for discriminators
Go
1
star
26

akutz

1
star
27

git-hook-lib

The git-hook-lib framework enables branch name and commit message validation as well as branch and file permissions for client and server git hooks.
Ruby
1
star
28

github-impact

Investigates the impact of one or more users on a GitHub project
Go
1
star
29

vmctf

Terraform scripts for deploying systems to VMware Cloud (VMC) on AWS
HCL
1
star
30

gosync

A synchronization library for Go.
Go
1
star