Go · Stage 1 – Storing Information · Lesson 11 of 64

Static Typing

Understand why a Go variable keeps one type for its entire lifetime.

Go checks assignments before producing a runnable program:

Go code

score := 10
score = 20
fmt.Println(score)
Line-by-line explanation
  1. The short declaration creates score as an int.
  2. The second line assigns another int, which is allowed.
  3. Println displays the new value.
Expected output
20

Both assigned values have the same integer type.

This version does not compile:

Broken Go code

score := 10
score = "high"
Line-by-line explanation
  1. Score is created as an int.
  2. The second line tries to assign a string.
  3. The compiler rejects the type mismatch.
Expected output
cannot use "high" (untyped string constant) as int value in assignment

The exact wording can vary slightly by Go version, but it identifies string versus int.

Python variables can point to different types at different moments. Go instead makes the type stable and asks you to convert deliberately when needed.