Operators are symbols that tell the compiler to perform a specific mathematical or logical operation. Go’s built-in symbols include

  • Arithmetic operator
  • Relational operator
  • Logical operator
  • An operator
  • The assignment operator

Take logical operators as an example, as shown in the following table

Operation type Go Operator
AND &&
OR
NOT

Take bitwise operators, as shown in the following table

Operation type Go Operator Sample
AND & p & q
OR | p | q
XOR ^ p ^ q
NOT ~ ~p
Left shift << p << 1
Right shift >> p >> 2

The assignment operator = + = – = * = / = % = < < = > > = ^ = | =

Other operators

Go Operator Note
& return the address of a variable
* pointer to a variable

Precedence of the operator precedence Determines the grouping of an expression and affects its solution. The order from top to bottom is shown below

Category Operator Associativity
Postfix () [] ->. ++ – Left to right
Unary + -! ~ ++ – – (type)* & sizeof Right to left
Multiplicative * / % Left to right
Additive + – Left to right
Shift << >> Left to right
Relational < < = > > = Left to right
Equality = =! = Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR
Assignment = += -= *= /= %=>>= <<= &= ^= =
Comma . Left to right