Beginner Fundamentals

Operators

Operators let you combine and compare values. Go supports the common categories you would expect.

Arithmetic

a, b := 10, 3
fmt.Println(a + b) // 13
fmt.Println(a - b) // 7
fmt.Println(a * b) // 30
fmt.Println(a / b) // 3 (integer division)
fmt.Println(a % b) // 1 (remainder)

Comparison

These return a bool.

fmt.Println(a == b) // false
fmt.Println(a != b) // true
fmt.Println(a > b)  // true
fmt.Println(a <= b) // false

Logical

Used to combine boolean values.

t, f := true, false
fmt.Println(t && f) // false (AND)
fmt.Println(t || f) // true  (OR)
fmt.Println(!t)     // false (NOT)

Assignment

x := 5
x += 2 // x = 7
x -= 1 // x = 6
x *= 3 // x = 18
x /= 2 // x = 9
fmt.Println(x)

There is no ++x, but x++ and x-- work as statements:

x++
fmt.Println(x) // 10