Go language operators
Go language operators
Operators are used to perform mathematical or logical operations while the program is running.
The built-in operators in Go language are:
Arithmetic operators
Relational operators
Logical operators
Bitwise operators
Assignment operators
Other operators
Let’s take a detailed look at the introduction of each operator.
Arithmetic operators
The following table lists all Go language arithmetic operators. Suppose A has a value of 10 and B has a value of 20.
Operator | Description | Instance |
---|---|---|
+ | Addition | A + B output result 30 |
- | Subtraction | A - B output result-10 |
* | Multiply | A * B Output result 200 |
/ | Division | B / A Output result 2 |
Find remainder | B % A Output result 0 | |
increment | A++ output result 11 | |
自Minus | A--Output result 9 |
package main import "fmt" func main() { var a int = 21 var b int = 10 var c int c = a + b fmt.Printf("第一行 - c 的值为 %d\n", c ) c = a - b fmt.Printf("第二行 - c 的值为 %d\n", c ) c = a * b fmt.Printf("第三行 - c 的值为 %d\n", c ) c = a / b fmt.Printf("第四行 - c 的值为 %d\n", c ) c = a % b fmt.Printf("第五行 - c 的值为 %d\n", c ) a++ fmt.Printf("第六行 - c 的值为 %d\n", a ) a-- fmt.Printf("第七行 - c 的值为 %d\n", a ) }The above example running results :
第一行 - c 的值为 31 第二行 - c 的值为 11 第三行 - c 的值为 210 第四行 - c 的值为 2 第五行 - c 的值为 1 第六行 - c 的值为 22 第七行 - c 的值为 21
Relational operatorsThe following table lists all the relational operators of the Go language. Suppose A has a value of 10 and B has a value of 20.
Description | Instance | |||
---|---|---|---|---|
Check whether the two values are equal, return True if they are equal, otherwise return False. | (A == B) is False | |||
Check whether the two values are not equal, return True if they are not equal, otherwise return False . | (A != B) is True | |||
Check whether the left value is greater than the right value, if so, return True, otherwise return False. | (A > B) is False | |||
Check whether the left value is less than the right value, if so, return True, otherwise return False. | (A < B) is True | |||
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. | (A >= B) is False | |||
Check whether the value on the left is less than or equal to the value on the right, if it is, return True otherwise Return False. | (A <= B) is True |
Operator | Description | Instance |
---|---|---|
&& | Logical AND operator. The condition is True if both operands are True, otherwise False. | (A && B) is False |
|| | logical OR operator. The condition is True if both operands have a True, otherwise it is False. | (A || B) is True |
! | logical NOT operator. Logical NOT condition False if the condition is True, True otherwise. | !(A && B) is True |
The following example demonstrates the usage of logical operators:
package main import "fmt" func main() { var a bool = true var b bool = false if ( a && b ) { fmt.Printf("第一行 - 条件为 true\n" ) } if ( a || b ) { fmt.Printf("第二行 - 条件为 true\n" ) } /* 修改 a 和 b 的值 */ a = false b = true if ( a && b ) { fmt.Printf("第三行 - 条件为 true\n" ) } else { fmt.Printf("第三行 - 条件为 false\n" ) } if ( !(a && b) ) { fmt.Printf("第四行 - 条件为 true\n" ) } }
The above example running results :
第二行 - 条件为 true 第三行 - 条件为 false 第四行 - 条件为 true
Bit operators
Bit operators operate on the binary bits of integers in memory.
The following table lists the calculations of the bit operators &, |, and ^:
q | p & q | p | q | p ^ q | |
---|---|---|---|---|
0 | 0 | 0 | 0 | |
1 | 0 | 1 | 1 | |
1 | 1 | 1 | 0 | |
0 | 0 | 1 | 1 |
Operator | Description | Instance |
---|---|---|
& | The bitwise AND operator "&" is a binary operator. Its function is to perform the binary AND of the corresponding two numbers involved in the operation. | (A & B) The result is 12, binary is 0000 1100 |
| | The bitwise OR operator "|" is a binary operation symbol. Its function is the corresponding binary phase of each of the two numbers involved in the operation or | (A | B). The result is 61, which in binary is 0011 1101 |
The bitwise XOR operator "^" is a binary operator. Its function is to perform the exclusive OR of the corresponding binary bits of the two numbers involved in the operation. When the two corresponding binary bits are different, the result is 1. | (A ^ B) The result is 49, which in binary is 0011 0001 | |
Left shift operator"<< " is a binary operator. Shifting n bits to the left is multiplying by 2 raised to the nth power. Its function is to shift all the binary bits of the operand on the left side of "<<" to the left by a certain number of bits. The number on the right side of "<<" specifies the number of shifted bits. The high bits are discarded and the low bits are filled with 0. | A << 2 The result is 240, binary is 1111 0000 | |
right shift operator">> ;" is a binary operator. Shifting n bits to the right is dividing by 2 raised to the nth power. Its function is to shift all the binary digits of the operand on the left of ">>" to the right by a certain number of digits, and the number on the right of ">>" specifies the number of digits to move. | A >> 2 The result is 15 , which in binary is 0000 1111 |
Operator | Description | Instance |
---|---|---|
= | Simple assignment operator, assigns the value of an expression to an lvalue | C = A + B assigns the result of the A + B expression to C |
+ = | After addition, assign value | C += A is equal to C = C + A |
-= | After subtraction Then assign | C -= A is equal to C = C - A |
*= | multiply and then assign | C * = A is equal to C = C * A |
/= | divide and then assign the value | C /= A is equal to C = C / A |
%= | Find the remainder and then assign the value | C %= A equals C = C % A |
< ;<= | Assign value after left shift | C <<= 2 is equal to C = C << 2 |
Assign value after right shift | C >>= 2 is equal to C = C >> 2 | |
Assignment after bitwise AND | C &= 2 is equal to C = C & 2 | |
Bitwise XOR Post-assignment | C ^= 2 is equal to C = C ^ 2 | ##|= |
C |= 2 is equal to C = C | 2 |
第 1 行 - = 运算符实例,c 值为 = 21 第 2 行 - += 运算符实例,c 值为 = 42 第 3 行 - -= 运算符实例,c 值为 = 21 第 4 行 - *= 运算符实例,c 值为 = 441 第 5 行 - /= 运算符实例,c 值为 = 21 第 6行 - <<= 运算符实例,c 值为 = 800 第 7 行 - >>= 运算符实例,c 值为 = 200 第 8 行 - &= 运算符实例,c 值为 = 0 第 9 行 - ^= 运算符实例,c 值为 = 2 第 10 行 - |= 运算符实例,c 值为 = 2
Other operators
The following table lists other operators in the Go language.
Instance | & | |
---|---|---|
&a; will give the actual address of the variable. | * | |
*a; is a pointer variable |
第 1 行 - a 变量类型为 = int 第 2 行 - b 变量类型为 = int32 第 3 行 - c 变量类型为 = float32 a 的值为 4 *ptr 为 4
Operator priority
Some operators have higher priority, and the operation direction of binary operators is from left to right. The following table lists all operators and their precedence, from top to bottom representing the order of priority:
7 | |
---|---|
6 | |
5 | |
4 | |
##3 | |
2 | |
##1 | ##|| |
## Of course, you can temporarily boost something by using parentheses The overall evaluation precedence of an expression. | The above instance running results: |