1, Basic classification of operators
- Arithmetic operator
- Assignment Operators
- Comparison operator
- Logical operator
- Bitwise Operators
- Other Operators
2, Arithmetic operator
Arithmetic operator is an operator that operates on numeric type variables.
(1) List of arithmetic operators
operator | operation | example | result |
+ | Plus sign | +8 | 8 |
- | minus sign | -8 | -8 |
+ | plus | 8+9 | 17 |
- | reduce | 8-2 | 6 |
* | ride | 8 * 2 | 16 |
/ | except | 8 / 2 | 4 |
% | Take mold | 8 % 3 | 2 |
++ | Self increasing | x = 3 x++ | 4 |
-- | Self subtraction | x = 3 x-- | 2 |
+ | String addition | "x" + "y" | "xy" |
(2) Case demonstration
package main import "fmt" func main() { // + - * / % ++ --case var a int = 12 var b int = 7 fmt.Println(a + b) // 19 fmt.Println(a - b) // 5 fmt.Println(a * b) // 84 fmt.Println(a / b) // 1 Redefine if you need to be a floating point number a Floating point number 12.0 var a float32 = 12.0 fmt.Println(a % b) // 5 a++ fmt.Println(a) // 13 a-- fmt.Println(a) //12 }
(3) Precautions
- The division sign "/". There is a difference between integer division and decimal division. The division between integers only retains the integer part and discards the decimal part
- When modulo a number, it can be equivalent to a% B = a - A / b * B
- In Go, self increasing and self decreasing can only be used as an independent language, not c = a + +, but only a++
- The + + and -- in Go can only be written after the variable, not in front of the variable, that is, there is only a + +, but no + + a
3, Assignment operator
Assignment operator is to assign the value of an operation to a specified variable.
(1) List of assignment operators
operator | describe | example |
= | Assign the value of an expression to an lvalue | c=a+b, assign the value of a+b to c |
+= | Add and assign | c+=a equals c=c+a |
-= | Subtraction before assignment | c-=a equals c=c-a |
*= | Multiply and assign | c*=a equals c=c*a |
/= | Divide and assign | c/=a equals c=c/a |
%= | Assign value after remainder | c%=a equals c=c%a |
<<= | Left shift assignment | C < < = 2 equals C = C < < 2 |
>>= | Assignment after shift right | c> > = 2 equals C = C > > 2 |
&= | Bitwise and post assignment | C & = 2 equals C = C & 2 |
^= | Assignment after bitwise XOR | c^=2 equals c=c^2 |
|= | Bitwise or post assignment | c|=2 equals c=c|2 |
(2) Case demonstration
package main import "fmt" func main() { // exchange a,b Value of two numbers a := 2 b := 5 fmt.Printf("Before exchange a=%v,b=%v",a,b) //Before exchange a=2,b=5 //Define an intermediate variable temp temp := a a = b b = temp fmt.Printf("After exchange a=%v,b=%v",a,b) //After exchange a=5,b=2 }
- The assignment operator evaluates from right to left
- The left side of an assignment operator can only be a time variable, and the right side can be a time variable, expression, or constant value
package main import "fmt" func main() { //exchange a,b The value of two numbers. Intermediate variables are not allowed a := 2 b := 5 a = a + b b = a - b // b = a + b - b a = a - b // a = a - (a-b) fmt.Printf("After exchange a=%v,b=%v",a,b) //After exchange a=5,b=2 }
package main import "fmt" func main() { // Find the maximum of the three numbers // First find the maximum of the two numbers, and then find the third one a := 10 b := 35 c := 25 var max int if a > b { max = a } else { max = b } fmt.Printf("The maximum value of the two numbers is%v", max) //35 if c > max { max = c } fmt.Printf("The maximum value of the three numbers is%v", max) //35 }
4, Relational operator
Relational operators are often used in conditional statements or loop structures of if structures, and their results are bool type (true/false).
(1) List of relational operators
operator | operation | Case list | result |
== | Equal to | 5==3 | false |
!= | Not equal to | 5!=3 | true |
< | less than | 5<3 | false |
> | greater than | 5>3 | true |
<= | Less than or equal to | 5<=3 | false |
>= | Greater than or equal to | 5>=3 | true |
(2) Case demonstration
package main import "fmt" func main() { var a int = 5 var b int = 3 fmt.Println(a==b) //false fmt.Println(a!=b) //true fmt.Println(a<b) //false fmt.Println(a>b) //true fmt.Println(a<=b) //false fmt.Println(a>=b) //true }
5, Logical operator
It is used to connect multiple relational expressions, and the result is also a value of bool type.
(1) List of logical operators
Suppose the value of A is true and the value of B is false:
operator | explain | example |
&& | Logic and operators. If both sides are true, the result is true, otherwise it is false | A & & B is false |
|| | Logical or operator. If one of the operands on both sides is true, it is true; otherwise, it is false | A | B is true |
! | Logical non operator. false if the condition is true, otherwise true | ! (A & & B) is true |
(2) Case demonstration
package main import "fmt" func main() { var A bool = true var B bool = false fmt.Println(A&&B) //false fmt.Println(A||B) // true fmt.Println(!(A&&B)) //true }
(3) Precautions
- &&It is also called short circuit and. If the first condition is false, the second condition will not judge the final result as false
- ||It is also called short circuit or. If the first condition is true, the second condition will not be judged, and the final result is true
6, Bitwise operator
Bit operators involve binary related contents, which are listed as follows:
operator | describe |
& | Bitwise and operator, the binary sum corresponding to each of the two numbers involved in the operation. The same is 1, the result is 1, otherwise it is 0 |
| | Bitwise OR operator, the binary phase or corresponding to each of the two numbers involved in the operation. One result is 1, otherwise it is 0 |
^ | Bitwise XOR operator, the binary XOR corresponding to each of the two numbers involved in the operation. When the binary is different, the result is 1, otherwise it is 0 |
<< | Shift left operator, which shifts the binary of the operand on the left of "< <" to the left by several bits, discards the high bit and fills the low bit with 0. Shifting n bits to the left is multiplied by 2 to the nth power |
>> | The shift left operator shifts the binary of the operand on the left of "> >" to the right by several bits, and the shift right by N bits is divided by the n power of 2 |
7, Other operators
(1) Pointer correlation
operator | describe | example |
& | Return variable storage address | &a. Gets the actual address of the variable |
* | Pointer variable | *a. Is a pointer variable |
(2) Ternary operator
Ternary operators are not supported in Go language. If you want to achieve the effect of ternary operators, you need to use conditional statements to implement them.
if <condition> { //... } else { //... }
8, Operator priority
classification | describe | Relevance | priority |
suffix | () [] ++ -- | From left to right | high |
Monocular | + - ! ~ (type) * & sizeof | Right to left | ........ |
multiplication | * / % | From left to right | |
addition | + - | From left to right | |
displacement | << >> | From left to right | |
relationship | < <= > >= | From left to right | |
equal | == != | From left to right | |
Bitwise AND | & | From left to right | |
Bitwise XOR | ^ | From left to right | |
Bitwise OR | | | From left to right | |
Logic and | && | From left to right | |
Logical or | || | From left to right | |
Assignment Operators | = += -= *= /= %= >>= <<= &= ^= |= | Right to left | |
comma | , | From left to right | low |
- In the above table, the priority decreases in turn
- Only unary operators and assignment operators operate from right to left