Go Operators
Operators perform operations on values: arithmetic, comparisons, logic and bit manipulation. Go's operators look familiar if you know C, Java or JavaScript, with a few deliberate simplifications — for example, ++ and -- are statements, not expressions.
Arithmetic operators
| Operator | Name | Example (a := 17, b := 5) |
Result |
|---|---|---|---|
+ |
addition | a + b |
22 |
- |
subtraction | a - b |
12 |
* |
multiplication | a * b |
85 |
/ |
division | a / b |
3 (integer division) |
% |
remainder (modulus) | a % b |
2 |
++ |
increment | a++ |
a becomes 18 |
-- |
decrement | a-- |
a becomes 16 |
package main
import "fmt"
func main() {
a, b := 17, 5
fmt.Println(a+b, a-b, a*b, a/b, a%b)
x, y := 17.0, 5.0
fmt.Println(x / y)
a++
b--
fmt.Println(a, b)
greeting := "Go" + "pher" // + also joins strings
fmt.Println(greeting)
}22 12 85 3 2
3.4
18 4
Gopher
a++ cannot be used inside another expression — b := a++ and ++a do not compile. This removes a whole class of confusing code.
Assignment operators
| Operator | Same as |
|---|---|
x = 5 |
assign |
x += 3 |
x = x + 3 |
x -= 3 |
x = x - 3 |
x *= 3 |
x = x * 3 |
x /= 3 |
x = x / 3 |
x %= 3 |
x = x % 3 |
x &= 3, x |= 3, x ^= 3 |
bitwise AND / OR / XOR assignment |
x <<= 2, x >>= 2 |
shift assignment |
package main
import "fmt"
func main() {
balance := 100
balance += 50
balance -= 30
balance *= 2
balance /= 4
balance %= 7
fmt.Println(balance)
msg := "Hello"
msg += ", world"
fmt.Println(msg)
}4
Hello, world
Comparison operators
They compare two values of the same type and produce a bool: ==, !=, <, <=, >, >=. Strings compare lexicographically (byte by byte).
Logical operators
| Operator | Meaning | Example |
|---|---|---|
&& |
true if both sides are true | age >= 18 && hasID |
|| |
true if at least one side is true | isAdmin || isOwner |
! |
inverts a boolean | !done |
Bitwise operators
Bitwise operators work on the individual bits of integers. They are common for flags, permissions and low-level protocols.
| Operator | Name | 12 op 10 (1100 op 1010) |
|---|---|---|
& |
AND | 8 (1000) |
| |
OR | 14 (1110) |
^ |
XOR | 6 (0110) |
&^ |
AND NOT (bit clear) | 4 (0100) |
<< |
left shift | 12 << 1 = 24 |
>> |
right shift | 12 >> 2 = 3 |
package main
import "fmt"
const (
Read = 1 << iota // 001
Write // 010
Exec // 100
)
func main() {
a, b := 12, 10
fmt.Println(a&b, a|b, a^b, a&^b, a<<1, a>>2)
perms := Read | Write
fmt.Printf("perms=%03b canWrite=%v canExec=%v\n", perms, perms&Write != 0, perms&Exec != 0)
perms &^= Write // remove the Write flag
fmt.Printf("perms=%03b\n", perms)
}8 14 6 4 24 3
perms=011 canWrite=true canExec=false
perms=001
The &^ operator ("AND NOT") is special to Go: it clears every bit in the left operand that is set in the right operand.
Operator precedence
Go has only five precedence levels for binary operators, from highest to lowest:
| Level | Operators |
|---|---|
| 5 | * / % << >> & &^ |
| 4 | + - | ^ |
| 3 | == != < <= > >= |
| 2 | && |
| 1 | || |
package main
import "fmt"
func main() {
fmt.Println(2 + 3*4) // multiplication first
fmt.Println((2 + 3) * 4) // parentheses override
fmt.Println(10-4-3) // left to right
fmt.Println(1+2 == 3 && 4 > 5 || true)
}14
20
3
true
Tip: When in doubt, add parentheses. They cost nothing and make your intent obvious to the next reader.
Test yourself
What does 19 % 4 evaluate to?
- 4
- 4.75
- 3
- 0
Which statement is valid Go?
y := x++++xx++fmt.Println(x++)
What is 6 &^ 3?
- 2
- 4
- 5
- 7