LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 1116 min read

Operators

Master Go's arithmetic, comparison, logical, and bitwise operators with practical examples.

Introduction

Operators let you combine and compare values — from basic arithmetic to bit-level manipulation. Go's operator set will feel familiar if you've used C, Java, or JavaScript, with a few Go-specific behaviors worth knowing.

Arithmetic Operators

Go supports the standard arithmetic operators: addition, subtraction, multiplication, division, and modulus (remainder).

package main
import "fmt"
func main() {
a, b := 17, 5
fmt.Println(a + b) // 22
fmt.Println(a - b) // 12
fmt.Println(a * b) // 85
fmt.Println(a / b) // 3 (integer division truncates)
fmt.Println(a % b) // 2 (remainder)
}
Output

Click Run to see what this code prints.

Integer Division Truncates

When both operands of / are integers, Go performs integer division and discards the remainder. 17 / 5 gives 3, not 3.4. Convert one operand to a float (e.g. float64(a) / float64(b)) if you need a decimal result.

Comparison Operators

Comparison operators compare two values and produce a `bool` result: `==`, `!=`, `<`, `>`, `<=`, `>=`.

package main
import "fmt"
func main() {
x, y := 10, 20
fmt.Println(x == y) // false
fmt.Println(x != y) // true
fmt.Println(x < y) // true
fmt.Println(x >= 10) // true
}
Output

Click Run to see what this code prints.

Logical Operators

Logical operators combine boolean expressions: `&&` (AND), `||` (OR), and `!` (NOT). Go uses short-circuit evaluation, meaning the right-hand side of `&&` or `||` is only evaluated if necessary.

package main
import "fmt"
func main() {
age := 25
hasLicense := true
canDrive := age >= 18 && hasLicense
fmt.Println(canDrive)
isWeekend := false
isHoliday := true
isDayOff := isWeekend || isHoliday
fmt.Println(isDayOff)
fmt.Println(!isWeekend)
}
Output

Click Run to see what this code prints.

Bitwise Operators

Bitwise operators manipulate individual bits of integer values: `&` (AND), `|` (OR), `^` (XOR when binary, or unary NOT), `&^` (AND NOT / bit clear), `<<` (left shift), and `>>` (right shift). These are common when working with flags, masks, or low-level data.

package main
import "fmt"
func main() {
a := 12 // 1100 in binary
b := 10 // 1010 in binary
fmt.Println(a & b) // 8 (1000)
fmt.Println(a | b) // 14 (1110)
fmt.Println(a ^ b) // 6 (0110)
fmt.Println(a << 1) // 24 (shift left = multiply by 2)
fmt.Println(a >> 1) // 6 (shift right = divide by 2)
}
Output

Click Run to see what this code prints.

Assignment Operators

Go supports compound assignment operators that combine an operation with assignment, so you don't have to repeat the variable name.

OperatorEquivalent To
x += 5x = x + 5
x -= 5x = x - 5
x *= 2x = x * 2
x /= 2x = x / 2
x++x = x + 1
x--x = x - 1
No Prefix ++ / --

Go only supports x++ and x-- as standalone statements — there is no prefix form (++x), and neither can be used inside a larger expression like fmt.Println(x++). This is intentional; it keeps increment/decrement simple and unambiguous.

Operator Precedence

Like most languages, Go evaluates multiplication and division before addition and subtraction, and comparison before logical operators. When in doubt, use parentheses to make the intended order explicit and easier to read.

package main
import "fmt"
func main() {
result := 2 + 3*4 // multiplication first: 2 + 12
fmt.Println(result) // 14
clearer := 2 + (3 * 4) // same result, but explicit
fmt.Println(clearer) // 14
}
Output

Click Run to see what this code prints.

Common Mistakes

Avoid These Mistakes
  • Expecting `/` to return a decimal when both operands are integers — it truncates instead.
  • Trying to use `++` or `--` as an expression (e.g. inside fmt.Println) — Go only allows them as standalone statements.
  • Forgetting that `&&` and `||` short-circuit, which matters if the right-hand side has side effects.
  • Mixing up `&` (bitwise AND) with `&&` (logical AND) — they operate on entirely different kinds of values.

Best Practices

  • Convert to float64 explicitly whenever you need a precise (non-truncated) division result.
  • Use parentheses to clarify precedence in any expression that mixes multiple operator types.
  • Prefer compound assignment operators (+=, -=, etc.) over verbose repeated assignment for readability.
  • Reach for bitwise operators only when you genuinely need bit-level control (flags, masks, low-level protocols).

Frequently Asked Questions

Go's designers deliberately left it out to keep the language simple and to avoid encouraging overly dense, hard-to-read one-liners. Use a regular if/else statement instead — covered in the next lesson.

Convert at least one operand to float64 before dividing, e.g. float64(a) / float64(b), otherwise Go performs integer division and truncates the result.

No. Go only supports the postfix forms x++ and x--, and only as standalone statements, not as part of a larger expression.

Key Takeaways

  • Arithmetic operators include +, -, *, /, and % (modulus); integer division truncates.
  • Comparison operators (==, !=, <, >, <=, >=) always produce a bool.
  • Logical operators (&&, ||, !) short-circuit and combine boolean expressions.
  • Bitwise operators (&, |, ^, <<, >>, &^) manipulate individual bits.
  • x++ and x-- are statements only, with no prefix form and no ternary operator in Go.

Summary

You now know Go's full operator toolkit — arithmetic, comparison, logical, and bitwise. Next, you'll learn how to get data in and out of your programs using fmt and bufio.

Next Lesson →

Input & Output