Go · Stage 2 – Doing Things with Values · Lesson 15 of 64

Math Operators

Use Go symbols to perform basic calculations.

Go code

total := 6 + 2
fmt.Println(total)
Line-by-line explanation
  1. The short declaration creates total.
  2. The plus operator adds 6 and 2.
  3. Println displays the result.
Expected output
8

This is the expected result.

Go uses - for subtraction, * for multiplication, / for division, and % for the remainder. Integer division discards the fractional part.

Go code

answer := 7 / 2
fmt.Println(answer)
Line-by-line explanation
  1. Both values are integers.
  2. Integer division produces 3, not 3.5.
  3. The result is displayed.
Expected output
3

This is the expected result.