Dev Skills
Go·Section 5 of 15 · 3 lessons · about 2 h 30 min

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.

  1. 01Generics
    Write one function for many types, and know when an interface is the better answer.
    Graded challenge
  2. 02Iterators and range over functions
    What `range` can loop over since Go 1.22 and 1.23, and why `maps.Keys` returns a function.
    Graded challenge
  3. 03The standard library
    The 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.Ordered and ~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

  1. 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.
  2. Then read the lessons in order: Generics, then Iterators (iter.Seq[V] is itself a generic type), then The standard library. Do the graded Max challenge in Generics before you move on.
  3. Next comes Web services, which leans on encoding/json and log/slog from here and on context from 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. 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 | ~float64 accepts.

    Remember: If the duplication is between behaviours, you wanted an interface, not a generic.

  2. 2. Iterators and range over functions

    • Write a loop with range over an integer.
    • Write an iter.Seq[int] function that stops when yield returns false.
    • Predict what printing maps.Keys(m) shows, and fix it with slices.Sorted.

    Remember: An iterator is just a function you can range over. Always check what yield returns.

  3. 3. The standard library

    • Write a small program using strings, strconv and sort instead 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.

Test yourself