typestar

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.

Start this tour

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

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

Encore

  • lines.goCount lines, words, and bytes like wc, for files or stdin.

The other Go tours