Go Tutorial · Go Basics · Lesson 7 of 30

Go Operators

Arithmetic, comparison, logical and bitwise operators.

Operators

Kind Operators
Arithmetic + - * / %
Assignment = := += -= *= /= ++ --
Comparison == != < > <= >=
Logical && || !
Bitwise & | ^ &^ << >>
Example 1
package main

import "fmt"

func main() {
	a, b := 17, 5
	fmt.Println(a+b, a-b, a*b, a/b, a%b)
	a++
	fmt.Println(a)
	fmt.Println(a > b && b > 0, !(a == b))
	fmt.Println(6&3, 6|3, 6^3, 1<<4)
}

Output

22 12 85 3 2
18
true true
2 7 5 16

Run this on your computer — see “Get Started” for setup.

Notes:

  • ++ and -- are statements, not expressions: x++ is fine, y := x++ is not.
  • Integer division truncates toward zero.
  • There's no ternary operator ?: in Go — use an if.

Test yourself

Does Go have the ternary operator cond ? a : b?

  1. Yes
  2. No
Show answer

Answer: No