Operators are used to perform mathematical or logical operations while the program is running.
Operator
The operators built into Go are:
- Arithmetic operator
- Relational operator
- Logical Operators
- Bitwise operator
- Assignment operator
Arithmetic operator
Operator | description |
---|---|
+ | Add |
- | Subtraction |
* | Multiply |
/ | Divide |
% | Seek the remainder |
Note: ++
(Increment) and --
(Decrease) are separate statements in Go, not operators.
Relational operator
Operator | description |
---|---|
== | Check if the two values are equal, return True if they are equal or False otherwise. |
!= | Check if the two values are not equal, return True if they are not equal, otherwise return False. |
> | Check if the left value is greater than the right value, if it returns True, otherwise it returns False. |
>= | Check whether the left value is greater than or equal to the right value, if it returns True, otherwise it returns False. |
< | Check if the left value is less than the right value, if it returns True, otherwise it returns False. |
<= | Check if the left value is less than or equal to the right value, if it returns True, otherwise it returns False. |
Logical Operators
Operator | description |
---|---|
&& | Logical AND operator. True if the operands on both sides are True, otherwise False. |
|| | Logical OR operator. True if the operands on both sides are True, otherwise False. |
! | Logical NOT operator. If the condition is True, it is False, otherwise it is True. |
Bitwise operator
Bit operators operate on the binary bits of integers in memory.
Operator | description |
---|---|
& | The binary numbers corresponding to the two numbers involved in the operation are combined with each other. (1 is 1 for both) |
| | The binary phase OR corresponding to the two numbers involved in the operation. (One of the two is 1 is 1) |
^ | The binary numbers corresponding to the two numbers involved in the operation are respectively XOR, and when the two corresponding binary values are different, the result is 1. (1 if the two are different) |
<< | Shifting left by n bits is multiplied by 2 to the nth power. "A << b" shifts all binary bits of a to the left by b bits, discards the high bits, and fills in the low bits with 0s. |
>> | Shifting right by n bits is divided by 2 to the nth power. "A >> b" shifts all binary bits of a to the right by b bits. |
Assignment operator
Operator | description |
---|---|
= | Simple assignment operator to assign the value of an expression to an lvalue |
+= | Add after adding |
-= | Assign after subtraction |
*= | Multiply and then assign |
/= | Assign after division |
%= | Find the remainder before assigning |
<<= | Assignment after left shift |
>>= | Assignment after shifting to the right |
&= | Bitwise and post-assignment |
|= | Bitwise or post-assignment |
^= | Bitwise XOR assignment |
Reference materials: https://www.liwenzhou.com/posts/Go/go_menu/