This is the second day of my participation in the August More text Challenge. For details, see:August is more challenging

What is an operator?

Operators are used to perform program code operations on more than one operand item. For example, 2+3 has the operands 2 and 3 and the operator “+”. Golang’s operators have the same semantics as most language operators.

Arithmetic operator

+ - * / %(remainder) ++ --Copy the code

Note: ++ (increment) and — (decrement) are separate statements in Go and are not operators.

Example code:

func main(a) {
 a, b := 100.200
 fmt.Printf("A plus b = %d\n", a + b)
 fmt.Printf("A minus b = %d\n", a - b)
 fmt.Printf("A times b = %d\n", a * b)
 fmt.Printf("a 除 b = %d\n", a / b)
}
Copy the code

Relational operator

= =! = > < >= <=Copy the code
= = Check whether the two values are equal, returning True if they are, False otherwise.
! = Check that the two values are not equal, returning True if not False.
> Check whether the value on the left is greater than the value on the right. Return True if yes, False otherwise.
> = Check whether the value on the left is greater than or equal to the value on the right. Return True if yes, False otherwise.
< Check whether the value on the left is less than the value on the right. Return True if yes, False otherwise.
< = Check whether the value on the left is less than or equal to the value on the right. Return True if yes, False otherwise.

Example code:

func main(a) {
    a, b := 100.200
    //fmt.Println("a == b?", a == b)  // a == b? false
    //fmt.Println("a ! = b?" , a ! = b) // a ! = b? true
    //fmt.Println("a > b?", a > b )  // a > b? true
    //fmt.Println("a >= b?", a >= b)  // a >= b? false
    //fmt.Println("a < b?", a < b )  // a >= b? true
    //fmt.Println("a <= b?" , a <= b ) // a <= b? true
}
Copy the code

Logical operator

The operator describe
&& So called logical and operators. If both operands are non-zero, the condition becomes true
|| The logical OR operator. True if both operands have one True, False otherwise.
! So-called logical non-operators. Reverse the logical state of the operand. If the condition is true, then the logical result is false
func main(a) {
    a, b := true.false
    fmt.Println("a && b ?", a && b) // a && b ? false
    fmt.Println("a || b ?", a || b) // a || b ? true
    fmt.Println(! "" a ?", !a)  / /! a ? false
    fmt.Println(! "" b ?", !b)  / /! b ? true
}
Copy the code

An operator

The operator describe
& The corresponding binary phase and of the two numbers involved in the operation. (Only if both digits are 1)
| The binary phase or corresponding to the two numbers involved in the operation. (If one of the two digits is 1, it is 1)
^ The corresponding binary bits of the two numbers involved in the operation are different or, and when the corresponding binary bits are different, the result is 1. (1 if two are different)
<< So if you move n to the left, that’s 2 to the n. “A <<b” is to shift all binary bits of A to the left by b bits, discarding the high bits and adding 0 to the low bits.
>> If we move n to the right, we’re dividing by 2 to the n. “A >>b” is to shift all the binary bits of a to the right by b bits.

Assignment operator

The operator describe The sample
= Simple assignment operator that assigns values from the operand to the left of the right-hand operand C = A + B will assign the value of A + B to C
+ = The add and assign operator, which increments the right and left operand and assigns the resulting left operand C plus A is the same thing as C equals C plus A
– = The subtraction and assignment operator, which subtracts the right-hand operand from the left-hand operand and assigns the resulting left-hand operand C minus A is the same thing as C equals C minus A
* = The multiplication and assignment operator, which multiplies the right-hand operand with the left-hand operand and assigns the resulting left-hand operand C times A is the same thing as C equals C times A
/ = The division assignment operator that pairs the left operand with the right and assigns the resulting left operand C/A is the same thing as C = C/A
% = The modulus and assignment operator, which needs to use the modulus of both operands and allocate the resulting left-hand operand C %= A equals C = C % A
< < = The left shift and assignment operator C <<= 2 is the same as C = C << 2
= Shift to the right and assign operator C >>= 2 is the same as C = C >> 2
& = Bitwise and assignment operators C&2 is the same thing as C = c&2
^ = The bitwise Xor assignment operator C ^= 2 is the same thing as C = C ^ 2

Operator precedence

Some operators have higher precedence, and binary operators operate from left to right. The following table lists all the operators and their priorities, from top to bottom representing the highest to lowest priority:

priority The operator
7 ~! ++ —
6 * / % << >> & &^
5 + – ^
4 = =! = < <= >= >
3 <-
2 &&
1 \

Of course, you can temporarily elevate the overall priority of an expression by using parentheses.