↓ Skip to main content

Operators in Go

714 words·4 mins· loading · loading ·
Table of Contents
Learn Golang - This article is part of a series.
Part 7: This Article

Updated 16 Nov 2026: rewritten. The original said Go has a ?: ternary operator, which it doesn’t, and its last example used variables it never declared; this version fixes both and adds bitwise operators.

Operators combine values into new values. This post goes through the ones Go has, the two that behave in a way people don’t expect, and the ones it leaves out on purpose.

Arithmetic
#

Go
package main

import "fmt"

func main() {
	a, b := 10, 3

	fmt.Println(a + b) // 13
	fmt.Println(a - b) // 7
	fmt.Println(a * b) // 30
	fmt.Println(a / b) // 3
	fmt.Println(a % b) // 1
}

Two things to know:

  • Dividing two integers gives an integer. 10 / 3 is 3, and the fractional part is discarded. To get 3.33..., convert at least one operand to a float: float64(a) / float64(b).
  • % is the remainder, and it takes the sign of the left operand: -7 % 3 is -1.

+ also joins strings: "go" + "lang" is "golang".

You can’t mix numeric types in one expression. a + 1.5 works only if a is a float64. If a is an int and you write a + f with f a float64, the compiler rejects it, and you convert one side explicitly.

Increment and decrement
#

x++ and x-- add or subtract one. In Go they are statements, not expressions: you can write x++ on its own line, but not y := x++, and there’s no ++x.

Comparison
#

Comparison operators compare two values of the same type and give a bool:

Go
a, b := 10, 3

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

Strings compare in dictionary (byte) order, so "apple" < "banana" is true.

Logical
#

&& (and), || (or) and ! (not) work on booleans:

Go
x, y := true, false

fmt.Println(x && y) // false
fmt.Println(x || y) // true
fmt.Println(!x)     // false

&& and || short-circuit: the right side is only evaluated if the left side doesn’t already decide the result. That lets you guard a risky check:

Go
if user != nil && user.Active {
	// user.Active is only read when user is not nil
}

Assignment
#

= assigns, and each arithmetic operator has a combined form:

Go
c := 5

c += 2 // c = c + 2  -> 7
c *= 3 // c = c * 3  -> 21
c %= 4 // c = c % 4  -> 1
fmt.Println(c)
1

The same pattern exists for -=, /=, and for the bitwise operators below.

Bitwise
#

Bitwise operators work on the individual bits of integers. You’ll meet them in flags, permissions and low-level code:

Operator Meaning Example Result
& AND 12 & 10 8
| OR 12 | 10 14
^ XOR 12 ^ 10 6
&^ AND NOT (clear bits) 12 &^ 10 4
<< shift left 1 << 3 8
>> shift right 16 >> 2 4

12 is 1100 in binary and 10 is 1010, which is how you get the results above.

Operators for pointers and channels
#

Three more operators are introduced in later posts:

  • &x gives the address of x, and *p reads the value a pointer p points to. See pointers.
  • <- sends and receives values on channels, which belong to concurrency and are outside this series.

What Go leaves out
#

Go has no ternary operator. Where other languages write x ? a : b, you write an if:

Go
a, b := 10, 3

res := ""
if a > b {
	res = "a > b"
} else {
	res = "a <= b"
}
fmt.Println(res)
a > b

It’s more lines, but there’s only ever one way to write a conditional, and it reads the same everywhere. Go also has no operator overloading and no implicit conversion between numeric types.

Precedence
#

When an expression has several operators, *, /, %, <<, >> and & bind tightest, then +, -, | and ^, then the comparisons, then &&, then ||. When in doubt, add parentheses: they cost nothing and the next reader will thank you.

Next: control flow with if, switch and for.

Learn Golang - This article is part of a series.
Part 7: This Article

Related