Operators
Learn about operators in Go.
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:
Press + to interact
package mainimport "fmt"func main() {fmt.Println(1 << 1 + 1) // (1 << 1) + 1 = 3}
...
Get hands-on with 1400+ tech skills courses.