typestar

Interfaces & generics

19 steps in 7 sets of Go.

Structs and methods, then interfaces, which Go satisfies implicitly. There is no implements keyword: if the methods match, the type qualifies, and that shapes how Go APIs are designed.

Then dynamic types and type switches, the standard interfaces worth knowing, generics as they finally arrived in 1.18, and a set on API shapes. Nineteen steps.

Start this tour

Structs & methods

Interfaces

Dynamic types

  • Type switchesOne switch over the dynamic type inside an interface value.
  • Type assertionsPulling a concrete type back out, safely or with a panic.

Standard interfaces

Generics

API shapes

  • Functional optionsThe Go answer to optional arguments: variadic functions that mutate a config.
  • Range over functionGo 1.23 lets a function be ranged over, which is how iterators work now.

Encore

  • wordcount.goA complete word-frequency program: read stdin, tally, sort, print.

The other Go tours