Operators of go language foundation

Time:2021-5-8

Operators are used to perform mathematical or logical operations while a program is running.

operator

The built-in operators of go language are:

  1. Arithmetic operator
  2. Relational operators
  3. Logical operators
  4. Bitwise Operators
  5. Assignment Operators

Arithmetic operator

operator describe
+ Add
subtract
* Multiply
/ be divided by
% Surplus

be careful: ++(self increasing) and--(self subtraction) is a separate statement in the go language, not an operator.

Relational operators

operator describe
== Check whether two values are equal. If they are equal, return true; otherwise, return false.
!= Check whether two values are not equal. If not, return true, otherwise return false.
> Check whether the value on the left is greater than the value on the right. If so, return true; otherwise, return false.
>= Check whether the value on the left is greater than or equal to the value on the right. If so, return true, otherwise return false.
< Check whether the left value is less than the right value, return true if it is, otherwise return false.
<= Check whether the value on the left is less than or equal to the value on the right. If so, return true, otherwise return false.

Logical operators

operator describe
&& Logical and operator. True if both operands are true, otherwise false.
|| Logical or operator. True if one of the operands on both sides is true, otherwise false.
! Logical not operator. False if condition is true, true otherwise.

Bitwise Operators

Bit operators operate on binary bits of integers in memory.

operator describe
& The binary phase and corresponding to the two numbers participating in the operation.
(only when both of them are 1 is 1)
| The binary phase or corresponding to each of the two numbers involved in the operation.
(if one of the two is 1, it is 1)
^ If the binary bits of the two numbers involved in the operation are different, or if the binary bits of the two numbers are different, the result is 1.
(1 if two are different)
<< To shift n bits to the left is to multiply by 2 to the nth power.
“a<
>> To shift n bits to the right is to divide by 2 to the nth power.
“A > > b” is to shift all the binary bits of a to the right.

Assignment Operators

 

operator describe
= A simple assignment operator that assigns the value of an expression to an lvalue
+= Add and assign
-= Add value after subtraction
*= Multiply and then assign the value
/= Assign value after division
%= Evaluate after remainder
<<= Assignment after left shift
>>= Assignment after right shift
&= Bitwise and post assignment
|= Bitwise or post assignment
^= Assignment after bitwise XOR