Go checks assignments before producing a runnable program:
Go code
score := 10
score = 20
fmt.Println(score)Line-by-line explanation
- The short declaration creates score as an int.
- The second line assigns another int, which is allowed.
- Println displays the new value.
Expected output
20Both assigned values have the same integer type.
This version does not compile:
Broken Go code
score := 10
score = "high"Line-by-line explanation
- Score is created as an int.
- The second line tries to assign a string.
- The compiler rejects the type mismatch.
Expected output
cannot use "high" (untyped string constant) as int value in assignmentThe 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.