GoIntermediate~7h

Go Foundations

Where Go’s answer is not yours

You can already program. This is the six places Go answers differently — a value that copies, a slice header that does not, a missing key that is simply zero, an interface nobody declares, a failure that is returned, and a call that waits for the function to end. Then you build a tool that needs all six.

Modules
7
Drills
13
Build steps
9
Time
~7h

Walk out able to

Say what a Go program prints before you run it, and ship a command-line tool whose failures are values rather than crashes.

What you work through

01

Types and zero values

Truncating conversions, and Go’s grammar from memory.

02

Slices, maps, structs

Who still shares the array after that write.

03

Interfaces, errors, defer

Method sets, wrapped failures, last in first out.

04

The depot tool

A command line that parses, totals and fails well.

The pitch

What you practise, and what you leave with

depot: a Go command-line tool that parses a manifest, refuses the rows it cannot trust, totals by destination and fails with a wrapped error.

You will practise

  • Go’s grammar, spelled from memory
  • Aliasing: who changed after that write
  • Method sets, and the nil that is not nil
  • Wrapped errors, and deferred calls in reverse

Afterwards you can

  • Predict what a Go program prints
  • Say which names still share an array
  • Decide if a type satisfies an interface
  • Return, wrap and match a failure

Modules

7 modules, 48 items

Lessons explain one idea. Drills repeat it until it sticks. Build steps make something that exists afterwards.

  1. 1

    Types, and the syntax around them

    ~1h

    Say any declaration’s type and starting value, and write Go’s grammar from memory.

    • LessonSee what this project is for4 min
    • LessonSee what a declaration decides7 min
    • LessonStart every type at its zero value7 min
    • LessonConvert it, and say what it lost7 min
    • LessonCount the bytes, not the letters6 min
    • DrillSay what the zero value does10 min
    • DrillConvert it yourself9 min
    • DrillCount the bytes, then the runes9 min
    • DrillWrap a fixed-width add8 min
    • DrillWrite the line from memory12 min
  2. 2

    Arrays copy, slices share

    ~47 min

    Say which names still see a write, and read length and capacity off any slice.

    • LessonLook at the header behind a slice7 min
    • LessonTell an array from a slice6 min
    • LessonSay what append actually promises7 min
    • LessonCut the share on purpose6 min
    • DrillSay who changed after that write12 min
    • DrillRead the length and the capacity9 min
  3. 3

    Maps and structs

    ~46 min

    Read an absent key without guessing, and say what an assignment copied.

    • LessonRead a key that is not there7 min
    • LessonNever depend on map order5 min
    • LessonSay what a struct assignment copied7 min
    • LessonPick the receiver that changes it6 min
    • DrillRead a key that is not there10 min
    • DrillSay what the copy kept11 min
  4. 4

    Interfaces nobody declares

    ~48 min

    Decide satisfaction from the method set, and spot the nil that is not nil.

    • LessonSatisfy an interface without saying so7 min
    • LessonDecide it from the method set7 min
    • LessonRead an interface as two words7 min
    • LessonGet the concrete type back out6 min
    • DrillDecide whether it satisfies it11 min
    • DrillSpot the nil that is not nil10 min
  5. 5

    Errors are values

    ~37 min

    Return, wrap and match a failure instead of throwing it somewhere else.

    • LessonReturn the failure, do not throw it7 min
    • LessonName the failure with a sentinel5 min
    • LessonWrap it and keep the original7 min
    • LessonAsk what actually failed7 min
    • DrillUnwrap it down to the sentinel11 min
  6. 6

    Deferred, in reverse

    ~36 min

    Say when a deferred call runs, in what order, and which value it was handed.

    • LessonSay when a deferred call runs6 min
    • LessonRun the deferred calls in reverse5 min
    • LessonSay which value it was handed7 min
    • LessonChange a named result on the way out6 min
    • DrillOrder the deferred calls12 min
  7. 7

    Build the depot tool

    ~2h

    Write a Go command-line tool that parses, totals, reports and fails cleanly.

    • LessonRead the specification for depot12 min
    • Build stepStart the module5 min
    • Build stepSave the manifest5 min
    • Build stepParse the rows you can trust14 min
    • Build stepTotal the kilos per destination12 min
    • Build stepSort it and draw the bars12 min
    • Build stepClose the file, and watch a defer9 min
    • Build stepFail like a Go program10 min
    • Build stepAdd the second format12 min
    • Build stepPoint it at your own data8 min

A taste of the code

share.go
page := make([]int, 0, 4)
page = append(page, 1, 2)

head := page[:1]
head = append(head, 99)

fmt.Println(page[1], len(page), len(head))

$ go run .

99 2 2

The six places Go answers differently.

The first item is free. ~7h of focused work, at your own pace.

Intermediate~7h7 modules