Search⌘ K
AI Features

Operators

Explore how Go's operator precedence differs from other languages and understand the unique treatment of increment, decrement, ternary absence, and bitwise operations. Learn how these affect writing clearer and less error-prone Go code within the context of data types.

Operator precedence

Go operator precedence is different than in other languages:

Precedence

Operator

5

*, /, %, <<, >>, &, &^

4

+, -, |, ^

3

==, !=, <, <=, >, >=

2

&&

1

||

Compare it to C based languages:

Precedence

Operator

10

*, /, %

9

+, -

8

<<, >>

7

<, <=, >, >=

6

==, !=

5

&

4

^

3

|

2

&&

1

||

This can lead to different results for the same expression:

In Go:

Go (1.16.5)
package main
import "fmt"
func main() {
fmt.Println(1 << 1 + 1) // (1 << 1) + 1 = 3
}
...