koans.app

Go Koans

Go is a small, statically typed, compiled language, built for simple and reliable software: fast builds, a garbage collector, and concurrency with goroutines and channels in the language itself. It runs much of the cloud, from Docker and Kubernetes to countless web services and command-line tools. These koans cover its types and zero values, strings and runes, slices and maps, structs, methods and interfaces, errors, generics and iterators, goroutines and channels, and testing itself.

73 koans in 17 groups, tested with testify. Each is a small program whose tests fail: replace every ___ until they pass.

Start the path

Awakening

Tests that fail until you fill them in, and Go's first ideas: variables, constants and types.

  1. Checks
  2. Variables and zero values
  3. Constants and iota
  4. Types and conversions

Numbers

Integer and floating-point arithmetic, sized integers, and converting numbers to text and back.

  1. Integers
  2. Floats
  3. Sized integers
  4. Numbers as text

Strings

Strings as bytes, the strings package, runes and UTF-8, and formatting with fmt.

  1. Strings
  2. The strings package
  3. Runes and UTF-8
  4. Formatting
  5. Building strings

Control flow

if, for (Go's one loop), switch, and labels.

  1. If
  2. For
  3. Switch
  4. Labels

Functions

Several results, variadic functions, closures and recursion.

  1. Functions
  2. Named results and variadic functions
  3. Closures
  4. Recursion

Arrays and slices

Fixed-size arrays, and slices: views of arrays that grow with append.

  1. Arrays
  2. Slices
  3. Slicing
  4. Append and copy
  5. The slices package

Maps

Maps from keys to values, sets, and maps shared by reference.

  1. Maps
  2. Going through a map
  3. Sets and struct keys
  4. Maps are shared

Structs

Grouping fields into structs, pointers, copies, and embedding.

  1. Structs
  2. Pointers
  3. Copies
  4. Embedding

Methods and interfaces

Methods on any type, value and pointer receivers, and interfaces that types satisfy without saying so.

  1. Methods
  2. Pointer receivers
  3. Interfaces
  4. Stringer
  5. Type assertions and switches
  6. Nil and interfaces

Errors

Errors as values: returning them, wrapping them, and finding them in a chain.

  1. Errors
  2. Wrapping errors
  3. Error types
  4. Joining errors

Defer, panic and recover

Calls deferred to the end of a function, and stopping a panic.

  1. Defer
  2. Panic and recover

Generics

Functions and types with type parameters, and the constraints that limit them.

  1. Generic functions
  2. Constraints
  3. Generic types
  4. Comparable

Iterators

Ranging over functions, the iter package's sequences, and building on them.

  1. Ranging over functions
  2. Sequences
  3. Lazy sequences

Goroutines and channels

Running functions concurrently, and passing values between them.

  1. Goroutines
  2. Channels
  3. Buffered channels
  4. Select
  5. Pipelines
  6. Context

The standard library

Sorting, time, JSON, regular expressions and reading text, from Go's own packages.

  1. Sorting
  2. Time
  3. JSON
  4. Regular expressions
  5. Reading text

Testing

Table-driven tests, subtests, assertions and helpers: how Go code is tested.

  1. Table-driven tests
  2. Assertions
  3. Helpers and cleanup

Puzzles

Small programs to read and predict: loop variables, shadowing, shared slices, defers and method sets.

  1. Loop variables
  2. Shadowing
  3. Shared arrays
  4. Deferred results
  5. Method sets
  6. Map values