Go - Operators (Overview)

Beginner 10/10 Teacher 10/10 Architect 10/10
Try it: Given a:=10, b:=3, print sum, difference, product, quotient, remainder.

Arithmetic

a, b := 10, 3
_ = a + b
_ = a - b
_ = a * b
_ = a / b
_ = a % b

Comparison & Logical

_ = a == b
_ = a != b
_ = a < b
_ = (a < b) && (b > 0)
_ = (a < b) || (b == 0)

Assignment & Bitwise

a += 2; a -= 2
x := 1 << 3 // 8

Common errors

  • Integer division rounding down: 7/2 == 3. Use float64 for precise division.

Practice

  • Compute a/b as float using float64(a)/float64(b).

Quick quiz

  1. What does == compare for strings?
Show answer Lexical equality of byte sequences (Unicode code points in UTF-8 strings).