Go Operators Explained with Simple Examples

Updated 6 min read
Go Operators featured image: cards showing arithmetic, comparison and logical operator symbols

Go operators are the symbols that let your program work with data, like adding prices or checking an age. In this post, you will learn the five groups you use every day. Each one comes with a short example that you can run and test yourself.

Before you start, make sure you can install Go and run your first program. Also, operators use the values from the last lesson. So a quick look at variables, constants and data types in Go will help. New to the language? Then begin with what Go is and why teams use it.

What Are Go Operators?

An operator is a symbol that performs an action on one or more values. The values it works on are called operands. For example, in a + b, the plus sign is the operator, while a and b are the operands.

Think of a calculator. The numbers are your data, and the buttons like plus or equals are the operators. In the same way, Go operators fall into five families: arithmetic, comparison, logical, assignment and bitwise. The diagram below shows all five side by side.

Go Operators diagram showing the five operator families with symbols, examples and result types

Each family takes values and gives back a result: a number, a bool, or an updated variable.

Arithmetic Go Operators

Arithmetic operators do basic maths. You already know most of them from school, so this part is quick.

a := 10
b := 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

Also, the plus sign joins two strings. For example, "Go" + "Lang" gives you GoLang.

Integer Division and Remainder

Look closely at a / b above. The answer is 3, not 3.33, because both values are integers. So Go simply drops the decimal part. If you need the decimal, use float64 values instead.

x := 10.0
y := 3.0
fmt.Println(x / y) // 3.3333333333333335

The % sign, called modulus, gives the remainder after division. For example, it helps you check if a number is even, because n % 2 == 0 is true for every even number.

Increment and Decrement

count := 5
count++
fmt.Println(count) // 6
count--
fmt.Println(count) // 5

Here ++ adds one and -- takes one away. However, in Go these are statements, not values. So total := count++ fails with a syntax error. Also, Go has no ++count form at all.

Comparison Operators in Go

These Go operators check two values and always return a bool, which means true or false. Think of a security guard checking your age at a cinema. The answer is simply yes or no.

age := 20

fmt.Println(age == 20) // true  (equal)
fmt.Println(age != 20) // false (not equal)
fmt.Println(age < 18)  // false
fmt.Println(age >= 18) // true
fmt.Println("apple" < "banana") // true

Note the double equals sign. A single = stores a value, while == compares two values. Also, strings compare letter by letter, just like words in a dictionary.

Logical Go Operators

Logical operators join two or more true or false checks into one answer. As a result, you can test several conditions in a single line.

OperatorNameTrue when
&&ANDBoth sides are true
||ORAt least one side is true
!NOTThe value is false
hasTicket := true
isAdult := false

fmt.Println(hasTicket && isAdult) // false
fmt.Println(hasTicket || isAdult) // true
fmt.Println(!isAdult)             // true

Go also uses short-circuit evaluation. This means it stops checking as soon as it knows the answer. For example, in b != 0 && a/b > 2, Go skips the division when b is zero. So this pattern keeps your program safe from a crash.

Assignment Operators

You already met = and := in the last lesson. In addition, Go has shortcut forms that do maths and save the result in one step.

score := 10
score += 5 // same as score = score + 5, now 15
score -= 3 // 12
score *= 2 // 24
score /= 4 // 6
score %= 4 // 2

fmt.Println(score) // 2

These shortcuts keep your code short and clear. For instance, a shopping cart can use total += price each time the user adds an item.

Bitwise Operators in Brief

Bitwise operators work on the binary form of a number, which means its ones and zeros. You will not need them often as a beginner. Still, they appear in flags, permissions and fast maths, so it helps to know them.

a, b := 10, 3 // binary 1010 and 0011

fmt.Println(a & b)  // 2  (AND)
fmt.Println(a | b)  // 11 (OR)
fmt.Println(a ^ b)  // 9  (XOR)
fmt.Println(1 << 3) // 8  (shift left)
fmt.Println(a >> 1) // 5  (shift right)

A simple trick helps here. Shifting left by one doubles a number, and shifting right by one halves it.

Go Operators Precedence: Which Runs First?

Precedence is the order in which Go operators run inside a long expression. Just like school maths, multiplication runs before addition.

fmt.Println(2 + 3*4)   // 14
fmt.Println((2 + 3) * 4) // 20
OrderOperators
1 (first)* / % << >> & &^
2+ – | ^
3== != < <= > >=
4&&
5 (last)||

You do not need to memorise this table. Instead, add brackets whenever the order is not obvious, because brackets always run first and make your intent clear.

Common Mistakes with Go Operators

Error you seeWhy it happensHow to fix it
invalid operation: mismatched types int and float64You mixed two number types.Convert one side, for example float64(x).
invalid operation: division by zeroYou divided by a fixed 0.Check the value before dividing.
panic: runtime error: integer divide by zeroA variable became 0 while the program ran.Use if b != 0 before a / b.
syntax error: unexpected ++You used count++ as a value.Put count++ on its own line.

One more surprise for people coming from Java or JavaScript is that Go has no ternary operator. So x ? a : b does not exist, and you write a short if else instead. These details also come up in interviews, so try my Go interview questions and answers for practice.

Official Resources

These pages come straight from the Go team, so they are always accurate and up to date.

Quick Recap

  • Go operators come in five families: arithmetic, comparison, logical, assignment and bitwise.
  • Integer division drops the decimal, so 10 / 3 gives 3.
  • Comparison and logical operators always return true or false.
  • Shortcuts like += do maths and save the result in one step.
  • Use brackets to control the order, and remember that Go has no ternary operator.

What’s Next?

Now you can compare values and get true or false answers. Next, we will use those answers to make decisions in code with if, else and switch.

Coming soon: Go If Else and Switch Statements Explained