Generics and stdlib in Go (Golang)
Write one function that works for many types, and learn which built-in packages you will use most.
This section is about writing less code. Generics, added in Go 1.18, let one function or type work for many types: a single Max that handles int, float64 and your own type Celsius float64, instead of three copies that differ only in the type name. Iterators let a function hand values to a for ... range loop one at a time. Range over functions arrived in Go 1.23, and it is why maps.Keys now returns a function rather than a slice.
The third lesson is a tour of the standard library packages you will reach for most: strings, strconv, time, slices, maps, errors, encoding/json, log/slog, crypto/rand and more. A lot of beginner Go code is a hand-written helper for something the standard library already does, tested and fast.
Generics are also the feature people overuse most, so the first lesson spends as much time on when not to use them, and when an interface is the better answer, as on the syntax.
- 01GenericsWrite one function for many types, and know when an interface is the better answer.Graded challenge
- 02Iterators and range over functionsWhat `range` can loop over since Go 1.22 and 1.23, and why `maps.Keys` returns a function.Graded challenge
- 03The standard libraryThe packages that already do what you were about to write, including an HTTP server with routing and middleware.Graded challenge
What you will be able to do
- Write and read generic functions, including constraints such as
cmp.Orderedand~int | ~float64. - Choose between a type parameter and an interface, and explain why a generic
Repository[T]usually costs more than it saves. - Write your own iterator, and turn any iterator into a sorted slice with
slices.Sorted. - Find the standard library function that replaces a loop you were about to write.
The order to study it in
- Finish Core language first, especially functions, slices, maps and interfaces. Generics only make sense once you have felt the duplication they remove, and the comparison with interfaces assumes you have written some.
- Then read the lessons in order: Generics, then Iterators (
iter.Seq[V]is itself a generic type), then The standard library. Do the gradedMaxchallenge in Generics before you move on. - Next comes Web services, which leans on
encoding/jsonandlog/slogfrom here and oncontextfrom Concurrency.
What each lesson covers
By the end of this section you will be able to do everything below. Each lesson has runnable Go examples you can edit and run in the browser.
1. Generics
- Write a generic function with a type parameter and a constraint.
- Explain when an interface is a better choice than a type parameter.
- Predict which types a constraint like
~int | ~float64accepts.
Remember: If the duplication is between behaviours, you wanted an interface, not a generic.
2. Iterators and range over functions
- Write a loop with
rangeover an integer. - Write an
iter.Seq[int]function that stops whenyieldreturns false. - Predict what printing
maps.Keys(m)shows, and fix it withslices.Sorted.
Remember: An iterator is just a function you can range over. Always check what yield returns.
- Write a loop with
3. The standard library
- Write a small program using
strings,strconvandsortinstead of hand-rolled loops. - Explain why reaching for the standard library first saves bugs.
- Fix a hand-written helper by replacing it with the stdlib function that already does it.
Remember: Check the standard library before you write a helper. It is usually already there.
- Write a small program using