Path/Stage 1 · Foundations

Variables & Types

Declare, infer, and format Go's basic types.

Gopher narrator

Have the gopher read this explanation out loud.

Declare with var x int = 5 or the short form x := 5, which infers the type from the value. Basic types: int, float64, string, bool.

fmt.Printf formats with verbs: %d (int), %s (string), %f/%.2f (float), %v (any value), %t (bool). Printf does not add a trailing newline — use \n yourself.

var name string = "Ada"
age := 30       // int inferred
pi := 3.14      // float64 inferred
fmt.Printf("%s is %d\n", name, age)
Exercise 1·code

Implement statLine(name string, level, hp int) string that returns a stat line like "Ember Lv1 HP42" — no spaces around Lv or HP. A hidden test suite exercises it with several dragons.

Plate · main.go
Loading...
Test suite · go test
  • TestStatEmberpending
  • TestStatHighLevelpending
  • TestStatFaintedpending
Hint

fmt.Sprintf builds a string from a format template — same verbs as Printf.

Exercise 2·code

Implement describe(temp float64) string that returns:

  • "freezing" when temp <= 0
  • "cold" when 0 < temp < 15
  • "warm" when 15 <= temp < 28
  • "hot" when temp >= 28

A hidden test suite covers each range plus boundary values.

Plate · main.go
Loading...
Test suite · go test
  • TestDescribeFreezingpending
  • TestDescribeColdpending
  • TestDescribeWarmBoundarypending
  • TestDescribeHotpending
Hint

Watch the boundary: temp == 15 is warm, temp == 28 is hot. Order your if chain so earlier branches don't swallow later ranges.

Exercise 3·mcq

What is the zero value of a string in Go?