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
a + b   // operator: +, operands: a and b, result: their sum!valid  // operator: !, operand: valid, result: logical negation

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.

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Remainder (modulo)a % b

The + operator also works for string concatenation:

Go
greeting := "Hello, " + "World!" // "Hello, World!"

Comparison operators

Comparison operators compare two values and return a bool.

OperatorDescriptionExample
==Equala == b
!=Not equala != b
<Less thana < b
<=Less than or equala <= b
>Greater thana > b
>=Greater than or equala >= b
Go
fmt.Println(10 == 10) // truefmt.Println(10 != 5)  // truefmt.Println(3 > 7)    // false

Logical operators

Logical operators combine or invert boolean expressions. Conditional logic is built from them.

OperatorDescriptionExample
&&AND: true if both operands are truea && b
||OR: true if at least one operand is truea || b
!NOT: inverts a boolean value!a
Go
x := 5fmt.Println(x > 0 && x < 10)  // true — x is between 0 and 10fmt.Println(x < 0 || x > 3)   // true — x is greater than 3fmt.Println(!(x == 5))         // false — x is 5, negated

Bitwise operators

Bitwise operators work directly on the binary representation of integers, one bit at a time.

OperatorDescriptionExample
&AND: bit is 1 only if both bits are 1a & b
|OR: bit is 1 if either bit is 1a | b
^XOR: bit is 1 if exactly one bit is 1a ^ b
&^AND NOT (bit clear): clears bits in a that are set in ba &^ b
Go
a := 0b1100 // 12b := 0b1010 // 10fmt.Println(a & b)  // 0b1000 = 8  — only the bit both sharefmt.Println(a | b)  // 0b1110 = 14 — all bits from eitherfmt.Println(a ^ b)  // 0b0110 = 6  — bits set in one but not bothfmt.Println(a &^ b) // 0b0100 = 4  — bits in a that b does not have

When used as a unary operator, ^ becomes bitwise complement and flips every bit:

Go
var x uint8 = 0b00001111fmt.Println(^x) // 0b11110000 = 240

Bit shift operators

Shift operators move the bits of an integer left or right by a specified number of positions.

OperatorDescriptionExample
<<Left shift: shift bits left, fill with zerosa << n
>>Right shift: shift bits righta >> n

Each left shift by 1 is equivalent to multiplying by 2. Each right shift by 1 is equivalent to dividing by 2:

Go
x := 1fmt.Println(x << 1) // 2  — 1 × 2¹fmt.Println(x << 2) // 4  — 1 × 2²fmt.Println(x << 3) // 8  — 1 × 2³y := 16fmt.Println(y >> 1) // 8  — 16 ÷ 2fmt.Println(y >> 2) // 4  — 16 ÷ 4

A practical use is building bit masks, patterns used to test, set or clear specific bits:

Go
const (    Read    = 1 << iota // 0001 — bit 0    Write               // 0010 — bit 1    Execute             // 0100 — bit 2)permissions := Read | Write  // 0011fmt.Println(permissions & Read)    // non-zero — Read is setfmt.Println(permissions & Execute) // 0 — Execute is not set

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, so you cannot use them in the middle of an expression or on the left side of an assignment:

Go
i := 5i++ // i is now 6i-- // i is now 5x := i++ // compile error: i++ is a statement, not an expression

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:

Go
x := 42p := &x // p is a *int pointing to x

* (dereference) accesses the value stored at a pointer address:

Go
fmt.Println(*p) // 42 — the value x holds*p = 100        // modifies x through the pointerfmt.Println(x)  // 100

<- (channel operator) sends a value into a channel or receives a value from one:

Go
ch := make(chan int)ch <- 42    // send 42 into the channelv := <-ch   // receive a value from the channel

Operator precedence

When an expression contains multiple operators, Go evaluates them in a defined order. An operator with higher precedence binds tighter, so Go evaluates it first.

PrecedenceOperators
5 (highest)* / % << >> & &^
4+ - | ^
3== != < <= > >=
2&&
1 (lowest)||
Go
2 + 3*4     // 14 — multiplication before additiontrue || false && false  // true — && binds tighter than ||

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:

Go
(2 + 3) * 4   // 20 — addition forced firstresult := (x > 0) && (x < 100)