Module 1 · Exercise 1 · ~30 min · Real Go code

Type Drills

Six small exercises in a real Go module. Make the failing tests pass. Each drill targets a concept from Module 1.

Where

The code lives at exercise-01-type-drills/ alongside this HTML. Open that directory in your terminal:

cd module-01-go-mental-model/exercises/exercise-01-type-drills
go test -v ./...

Implement the TODOs in drills.go. Re-run until all green.

What's in the module

FileWhat it is
go.modModule declaration. Tells Go this directory is a project.
drills.goSix drill skeletons. Each panics until you implement it.
drills_test.goTests that exercise the drills. Don't change.
README.mdQuick reference for what each drill teaches.

Drill index

  1. Drill 1 — Zero values. Return the zero value for six different types in one function.
  2. Drill 2 — Nil container semantics. Append to a nil slice (works), defensively-write to a nil map (don't panic — return an error).
  3. Drill 3 — Named types. Convert between two distinct named-string types. Reminds the type system that they aren't the same.
  4. Drill 4 — The nil-interface trap. Implement GoodSignal() so it returns a true nil error. BadSignal() demonstrates the trap and is left intentionally broken.
  5. Drill 5 — Counter with mutating methods. Pick the right receiver type. The wrong choice silently drops increments.
  6. Drill 6 — iota and String() on a named int. Add enum variants and a String() method.

Hints

When you're done

You should see something like:

=== RUN   TestDrill1_Zeros
--- PASS: TestDrill1_Zeros (0.00s)
=== RUN   TestAppendToNilSlice
--- PASS: TestAppendToNilSlice (0.00s)
... (8 more)
PASS
ok      example.com/typedrills    0.123s

Mark complete and move to Exercise 2 — method sets and interface satisfaction.