Akik Look at that coder
Public account: Look at that code farmer
The last installment introduced the Power link of Go language learning – Rotating list | Go theme month
- Rotating list
This article will continue to take you into the world of Go.
1. Introduction to this paper
Operator priority describes the order in which operations are performed when a computer computes an expression. The higher-priority operations are performed first, followed by the lower-priority operations. For example, we often talk about multiplying and dividing before adding and subtracting.
Operators built into the Go language
- Arithmetic operator
- Relational operator
- Logical operator
- An operator
- The assignment operator
- Other operators
2. Arithmetic operators
1. Arithmetic operator “+”
Add the two operands.
2. Arithmetic operator “-“
Subtract the two operands.
3. Arithmetic operator “*”
Multiply two operands.
4. Arithmetic operator ‘/’
Divide two operands.
5. Arithmetic operator ‘%’
To divide and mod two operands.
6. Arithmetic operator “++”
Increments the operator to increment an integer value by one.
7. Arithmetic operator ‘–‘
Incrementing the operator to subtract an integer value by one.
8. Arithmetic operator code demonstration
The specific code is shown in the following case:
package main
import (
"fmt"
)
func main(){
var nodeA int=10
var nodeB int=20
fmt.Printf("nodeA+nodeB=%v\n",nodeA+nodeB)
fmt.Printf("nodeA-nodeB=%v\n",nodeA-nodeB)
fmt.Printf("nodeA*nodeB=%v\n",nodeA*nodeB)
fmt.Printf("nodeA/nodeB=%v\n",nodeA/nodeB)
fmt.Printf("nodeA%%nodeB=%v\n",nodeA%nodeB)
nodeA++
nodeB--
fmt.Printf("nodeA++=%v\n",nodeA)
fmt.Printf("nodeB--=%v\n",nodeB)
}
Copy the code
The output is:
Special attention:
- In Go
++
and--
Can only be used as a statement, not an expression
a := 1
a ++ // Note: can't write ++ a or -- A must be used on the right side
// b := a++ //
Copy the code
Can't
use+ + since
or- the decrement
The operator initializes and assigns values to variables
For example, if this is an error case, the compiler will report an error:
package main
import "fmt"
func main(){
var a int = 10
var b int = a++
var c int = 20
c = a++
fmt.Print(a, b, c)
}
Copy the code
The output is:
The correct way to write this is:
package main
import "fmt"
func main(){
var a int = 10
var b int = 10
var c int
a++
c=a
fmt.Print(a, b, c)
}
Copy the code
The output is:
3. Relational operators
1. Relational operator ‘==’
Checks whether the values of the two operands are equal, and if they are, the condition is true.
2. The relational operator “! =”
Checks whether the values of the two operands are equal, and if they are not, the condition is true.
3. Relational operator ‘>’
Checks whether the value of the left-hand operand is greater than the value of the right-hand operand, if so, the condition is true.
4. Relational operator “<“
Checks whether the value of the left-hand operand is less than the value of the right-hand operand, if so, the condition is true.
5. Relational operator ‘>=’
Checks whether the value of the left-hand operand is greater than or equal to the value of the right-hand operand, if so, the condition is true.
6. Relational operator “<=”
Checks whether the value of the left-hand operand is less than or equal to the value of the right-hand operand, if so, the condition is true.
7. Relational operator code demonstration
The specific code is shown in the following case:
package main
import (
"fmt"
)
func main(){
var nodeA int=10
var nodeB int=20
fmt.Printf("NodeA = = nodeB? The answer is % v \ n",nodeA==nodeB)
fmt.Printf("nodeA! = nodeB? The answer is % v \ n",nodeA! =nodeB) fmt.Printf("NodeA > nodeB? The answer is % v \ n",nodeA>nodeB)
fmt.Printf("NodeA < nodeB? The answer is % v \ n",nodeA<nodeB)
fmt.Printf("NodeA > = nodeB? The answer is % v \ n",nodeA>=nodeB)
fmt.Printf("NodeA < = nodeB? The answer is % v \ n",nodeA<=nodeB)
}
Copy the code
The output is:
4. Logical operators
1. Logical operator “&&”
The logical AND operator. If neither operand is zero, the condition is true.
2. The logical operators “| |”
The logical OR operator. If either of the two operands is non-zero, the condition becomes true.
3. Logical operator “!”
Logical non-operators. Used to reverse the logical state of its operands. If the condition is true, the logical non-operator will be false.
4. Logical operator code demonstration
The specific code is shown in the following case:
package main
import (
"fmt"
)
func main(){
var nodeA bool=true
var nodeB bool=false
if nodeA&&nodeB{
fmt.Println("NodeA && nodeB is true")}else{
fmt.Println("NodeA && nodeB is false." ")}if nodeA||nodeB{
fmt.Println("NodeA | | nodeB is true")}else{
fmt.Println("NodeA | | nodeB is false." ")}if! (nodeA||nodeB){ fmt.Println(! "" (nodeA | | nodeB) as the true")}else{
fmt.Println(! "" (nodeA | | nodeB) to false")}}Copy the code
The output is:
5. Bit operators
Bitwise operators operate on the binary bits of integers in memory.
1. Bit operator “&”
The bitwise and operator “&” is the binocular operator. Its function is to participate in the operation of two numbers corresponding to the binary phase and.
2. The operator “|”
The bitwise or operator “|” is a binary operator. Its function is to participate in the operation of two numbers corresponding to the binary phase or
3. Bit operator “^”
The bitwise xor operator “^” is the binocular operator. Its function is that the corresponding binary digits of the two numbers involved in the operation are different or, when the corresponding binary digits are different, the result is 1.
4. Bit operator “<<“
The left-shift operator “<<” is the binocular operator. To the left n is 2 to the n. Its function is to “<<” left of all the binary operand left several bits, by “<<” right of the number to specify the number of moving, high discard, low fill 0.
5. Bit operator ‘>>’
The right-shift operator “>>” is the binocular operator. If you move to the right n places, you divide by 2 to the n. The function is to move all the binary digits of the operand to the left of “>>” several right, and the number to the right of “>>” specifies the number of digits to move.
6. Bit operator code demonstration
package main
import "fmt"
func main() {
var a int = 4 // Binary is: 100
var b int = 8 // Binary is: 1000
fmt.Printf("A&b value: %v\n",a&b) // The binary is: 000, and the corresponding decimal is 0. Note & is the operation of and corresponding to the upper and lower bits
fmt.Printf("A | b value is: % v \ n",a|b) // The binary is 1100, and the decimal is 12. Note & performs an or operation corresponding to the upper and lower bits
fmt.Printf("A ^b value: %v\n",a^b) // The binary is 1100, and the decimal is 12. The ^ bit operator is 1 if the upper and lower bits are different
fmt.Printf("A <<2 value: %v\n",a<<2) // The binary value is 10000, and the decimal value is 16. << n to the left is 2 to the n
fmt.Printf("B >>1 value: %v\n",b>>1) // The binary value is 100, and the decimal value is 4. >> Moving n places is dividing by 2 to the n
}
Copy the code
The output is:
5. Assignment operators
1. Assignment operator ‘=’
A simple assignment operator that assigns values from the right-hand operand to the left-hand operand.
2. Assignment operator ‘+=’
Add and assign operators that add the right operand to the left and assign the result to the left operand.
c+=a
fmt.Printf("C +=a: %v\n",c)
Copy the code
3. Assignment operator “-=”
The subtract and assignment operators subtract the right operand from the left and assign the result to the left operand.
c-=a
fmt.Printf("C -=a: %v\n",c)
Copy the code
4. Assignment operator “*=”
Multiplication and assignment operators that multiply the right operand by the left and assign the result to the left operand.
c*=a
fmt.Printf("C *=a: %v\n",c)
Copy the code
5. Assignment operator ‘/=’
The division and assignment operator that divides the left-hand operand with the right-hand operand and assigns the result to the left-hand operand.
c/=a
fmt.Printf("C /=a: %v\n",c)
Copy the code
6. Assignment operator ‘%=’
Modulo and assignment operators that take modulo from two operands and assign the result to the left-hand operand.
c%=a
fmt.Printf("C %%=a: %v\n",c)
Copy the code
7. Assignment operator “<<=”
Left shift and assignment operators.
c<<=1
fmt.Printf("C <<=1 value: %v\n",c)
Copy the code
8. Assignment operator ‘>>=’
Right shift and assignment operators.
c>>=1
fmt.Printf("C >>=1: %v\n",c)
Copy the code
9. Assignment operator “&=”
Bitwise and assignment operators.
c&=a
fmt.Printf("C &=a: %v\n",c)
Copy the code
10. Assignment operator “^=”
Bitwise xOR and assignment operators.
c^=a
fmt.Printf("C ^=a: %v\n",c)
Copy the code
11. The assignment operator “| =”
Contains the OR and assignment operators bitwise.
c|=a
fmt.Printf("C | = a value is: % v \ n",c)
Copy the code
6. Other operators
1. Operator “&”
Returns the address of the variable.
&a will give the actual address of variable A.
2. Operator “*”
Pointer to a variable.
* A is a pointer to the variable a.
7. Operator priority
Operator precedence determines the grouping in an expression. This affects how the expression is evaluated.
Some operators have higher precedence than others.
For example, multiplication and division operators have higher precedence than addition and subtraction operators.
The summary is as follows:
priority | classification | The operator | associativity |
---|---|---|---|
1 | Comma operator | . | From left to right |
2 | The assignment operator | =, +=, -=, *=, /=, %=, >=, <<=, &=, ^=, l= | From right to left |
3 | Logic or | ll | From left to right |
4 | Logic and | && | From left to right |
5 | Bitwise or | L or operation | From left to right |
6 | The bitwise exclusive or | ^ | From left to right |
7 | Bitwise and | & | From left to right |
8 | Equality/inequality | = =,! = | From left to right |
9 | Relational operator | <, <=, >, >= | From left to right |
10 | Displacement operator | < <, > > | From left to right |
11 | Addition/subtraction | +, – | From left to right |
12 | Multiply/divide/mod | * (multiplier), /, % | From left to right |
13 | Unary operator | ! , * (pointer), &, ++, –, + (plus sign), – (minus sign) | From right to left |
14 | Postfix operator | ( )、[ ]、-> | From left to right |
Note: The higher the priority value, the higher the priority!
Note: THE higher the priority value, the higher the priority!!
Note: The higher the priority value, the higher the priority!!
If you find this helpful:
1. Click “like” to support it, so that more people can see this article
2, pay attention to the public number: look at that code farmers, we study together and progress together.