Expressions and operands
An operator is a symbol that tells the compiler to perform a specific operation. The values the operator acts on are called operands, and the combination of operator and operands is called an expression.
Go operators come in two forms:
- Binary operators act on two operands — one on each side:
a + b,x == y - Unary operators act on a single operand, always written to the left:
!x,&v,-n
The two exceptions are ++ and --, which are written to the right (postfix): i++, i--.
Arithmetic operators
Arithmetic operators perform mathematical calculations on numeric types.
| Operator | Description | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Remainder (modulo) | a % b |
Integer division truncates
When both operands are integers, / performs integer division — the result is truncated toward zero, not rounded. 7 / 2 is 3, not 3.5. To get a float result, at least one operand must be a float: 7.0 / 2 gives 3.5.
The + operator also works for string concatenation:
Comparison operators
Comparison operators compare two values and return a bool.
| Operator | Description | Example |
|---|---|---|
== | Equal | a == b |
!= | Not equal | a != b |
< | Less than | a < b |
<= | Less than or equal | a <= b |
> | Greater than | a > b |
>= | Greater than or equal | a >= b |
Logical operators
Logical operators combine or invert boolean expressions. They are the building blocks of conditional logic.
| Operator | Description | Example |
|---|---|---|
&& | AND — true if both operands are true | a && b |
|| | OR — true if at least one operand is true | a || b |
! | NOT — inverts a boolean value | !a |
Short-circuit evaluation
&& and || use short-circuit evaluation. In a && b, if a is false, b is never evaluated — the result is already false. In a || b, if a is true, b is skipped. This matters when the right operand has side effects or could panic (such as a nil pointer dereference).
Bitwise operators
Bitwise operators work directly on the binary representation of integers, one bit at a time.
| Operator | Description | Example |
|---|---|---|
& | AND — bit is 1 only if both bits are 1 | a & b |
| | OR — bit is 1 if either bit is 1 | a | b |
^ | XOR — bit is 1 if exactly one bit is 1 | a ^ b |
&^ | AND NOT (bit clear) — clears bits in a that are set in b | a &^ b |
When used as a unary operator, ^ becomes bitwise complement — it flips every bit:
Bit shift operators
Shift operators move the bits of an integer left or right by a specified number of positions.
| Operator | Description | Example |
|---|---|---|
<< | Left shift — shift bits left, fill with zeros | a << n |
>> | Right shift — shift bits right | a >> n |
Each left shift by 1 is equivalent to multiplying by 2. Each right shift by 1 is equivalent to dividing by 2:
A practical use is constructing bit masks — patterns used to test, set, or clear specific bits:
Other operators
A few operators do not fit neatly into the categories above.
++ and -- increment and decrement a numeric variable by 1. In Go they are statements, not expressions — you cannot use them in the middle of an expression or on the left side of an assignment:
Unlike C and Java, Go only supports postfix form: i++ is valid, ++i is not.
& (address-of) returns the memory address of a variable, producing a pointer:
* (dereference) accesses the value stored at a pointer address:
<- (channel operator) sends a value into a channel or receives a value from one:
Operator precedence
When an expression contains multiple operators, Go evaluates them in a defined order. Higher precedence means the operator binds tighter — it is evaluated first.
| Precedence | Operators |
|---|---|
| 5 (highest) | * / % << >> & &^ |
| 4 | + - | ^ |
| 3 | == != < <= > >= |
| 2 | && |
| 1 (lowest) | || |
Unary operators have the highest precedence of all and always bind to their immediate operand.
When in doubt, use parentheses. They override precedence and make intent explicit:
No exponentiation operator
Go has no ** operator. For powers, use math.Pow(base, exp), which returns a float64. For integer powers, you need to write a loop or use bit shifting when the exponent is a power of two.