Language basics
30 steps in 6 sets of Go.
Go is small enough that thirty steps genuinely covers the language. Variables and types, control flow, functions, slices and maps, strings and formatting.
The smallness is the design, not an omission. There is usually one obvious way to do a thing, and gofmt settles the argument about how it looks before anyone can start it.
Variables & types
- Variables and constantsDeclaration forms, and formatted output.
- Zero valuesEvery declared variable starts at its type's zero value: no uninitialized memory.
- Constants and iotaUntyped constants and iota, which is how Go writes an enum.
- ConversionsGo converts explicitly: no implicit promotion, ever.
- PointersGo has pointers but no pointer arithmetic: they exist to share and to mutate.
Control flow
- Conditionals and switchBranching, including switch with no condition.
- switch, every formGo's switch takes no condition, several values per case, or an initializer.
- Labeled break and continueA label names the loop, so break and continue can leave the right one.
- FizzBuzzThe classic screening exercise for loops and conditionals.
Functions
- Functions and multiple returnsGo's signature style: values plus an error.
- Variadic functionsThe last parameter can take any number of arguments, arriving as a slice.
- ClosuresA function value captures the variables it uses, not their values.
- GCDEuclid's algorithm, still the shortest correct answer after two thousand years.
- Primality testTrial division up to the square root, which is enough.
- Iterative FibonacciFibonacci without recursion, two variables and a loop.
Arrays, slices & maps
- Arrays are valuesAn array has a fixed length in its type, and assigning one copies it.
- SlicesGo's growable views over arrays.
- Slices: length and capacityA slice is a view: pointer, length, capacity — and append may or may not copy.
- MapsKey-value storage and the comma-ok idiom.
- Working a mapThe comma-ok read, delete, and the deliberate randomness of iteration.
- Max of a sliceFinds the largest element, and shows why an empty slice needs a decision.
- Chunk a sliceSplits a slice into fixed-size pieces using slice expressions.
- Binary searchHalve the range each step; a thousand items takes ten comparisons.
- Word count with mapTallies word frequencies in a map, Go's built-in hash table.
Strings & formatting
- The strings packageTrimming, splitting, joining, and building text.
- Runes, bytes and stringsA string is bytes; ranging over it yields runes, and the two differ.
- Building stringsConcatenating in a loop allocates every time; a Builder does not.
- fmt verbsThe verb decides the rendering: value, quoted, type, or Go syntax.
- Reverse a stringReverses by runes rather than bytes, which is the only way that works for non-ASCII.
Encore
- lines.goCount lines, words, and bytes like wc, for files or stdin.