May 11, 2021 Go
Operators are used to perform mathematical or logical operations while the program is running.
The operators built into the Go language are:
Let's take a closer look at the description of the operators.
The following table lists arithmetic operators for all Go languages. Suppose the A value is 10 and the B value is 20.
Operator | Describe | Instance |
---|---|---|
+ | Add | The output of A-B is 30 |
- | Subtraction | A - B output -10 |
* | Multiply | A-B output 200 |
/ | Divide | B /A output 2 |
% | Ask for more | B % A output 0 |
++ | Self-increase | The output result is 11 |
-- | Self-reducing | A- Output 9 |
The following example demonstrates the use of individual arithmetic operators:
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 results of the above instance run:
第一行 - c 的值为 31 第二行 - c 的值为 11 第三行 - c 的值为 210 第四行 - c 的值为 2 第五行 - c 的值为 1 第六行 - c 的值为 22 第七行 - c 的值为 21
The following table lists the relationship operators for all Go languages. Suppose the A value is 10 and the B value is 20.
Operator | Describe | Instance |
---|---|---|
== | Check that the two values are equal, or false if equal return True. | False |
!= | Check that the two values are not equal, or false if true is not returned equally. | (A! . . B) is True |
> | Check that the value on the left is greater than the value on the right, or false if true is returned. | (A and B) is False |
< | Check that the value on the left is less than the value on the right, or false if true is returned. | (A slt; B) is True |
>= | Check that the value on the left is greater than or equal to the value on the right, or false if true is returned. | (A and B) is False |
<= | Check that the value on the left is less than or equal to the value on the right, or false if true is returned. | (A slt;?B) is True |
The following example demonstrates the use of relationship operators:
package main import "fmt" func main() { var a int = 21 var b int = 10 if( a == b ) { fmt.Printf("第一行 - a 等于 b\n" ) } else { fmt.Printf("第一行 - a 不等于 b\n" ) } if ( a < b ) { fmt.Printf("第二行 - a 小于 b\n" ) } else { fmt.Printf("第二行 - a 不小于 b\n" ) } if ( a > b ) { fmt.Printf("第三行 - a 大于 b\n" ) } else { fmt.Printf("第三行 - a 不大于 b\n" ) } /* Lets change value of a and b */ a = 5 b = 20 if ( a <= b ) { fmt.Printf("第四行 - a 小于等于 b\n" ) } if ( b >= a ) { fmt.Printf("第五行 - b 大于等于 b\n" ) } }
The results of the above instance run:
第一行 - a 不等于 b 第二行 - a 不小于 b 第三行 - a 大于 b 第四行 - a 小于等于 b 第五行 - b 大于等于 b
The following table lists the logical operators for all Go languages. Assume that the A value is True and the B value is False.
Operator | Describe | Instance |
---|---|---|
&& | Logical AND operator. I f the number of operations on both sides is True, the condition is True, otherwise false. | (A and B) is False |
|| | Logical OR operator. If the number of operations on both sides has a True, the condition is True, otherwise false. | (A || B ) true |
! | Logical NOT operator. If the condition is True, the logical NOT condition False, otherwise true. | ! ( A and B) is True |
The following example demonstrates the use 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 results of the above instance run:
第二行 - 条件为 true 第三行 - 条件为 false 第四行 - 条件为 true
Bit operators operate on binary bits of integers in memory.
The following table lists the bit operators, |, and calculations for :
P | Q | p & q | p | Q | p ^ q |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
Suppose A is 60; B = 13; I ts binary number is converted to:
A = 0011 1100 B = 0000 1101 ----------------- A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0001 ~A = 1100 0011
The bit operators supported by the C language are shown in the table below. Suppose A is 60 and B is 13:
Operator | Describe | Instance |
---|---|---|
& | Bit-by-bit and operator "and" are bipedal operators. I ts function is to participate in the operation of the two numbers corresponding to the two-bit phase. | The result is 12 and the binary is 0000 1100 |
| | By bit or operator "|" i s a bicep operator. Its function is to participate in the operation of the two numbers corresponding to the two-bit phase or | (A | B) The result is 61 and the binary is 0011 1101 |
^ | The bit-by-bit or operator """" is a bicep operator. Its function is that the two numbers involved in the operation correspond to each other, or when the two corresponding bits are different, the result is 1. | The result is 49 and the binary is 0011 0001 |
<< | The left-shift operator is a bipedal operator. M oving n bits to the left is the nth square multiplied by 2. I ts function shifts the two bits of the operation number on the left of the "lt;lt;" to several bits to the left, and specifies the number of moving digits by the number on the right side of the "lt;"lt;", discarding the high position, and filling 0 with the low bit. | The result is 240 and the binary is 1111 0000 |
>> | The right-shift operator, " T he n-bit shift to the right is divided by the n-square of 2. I ts function is to shift all two bits of the number of operations on the left of the " to the right, and the number to the right of the " specifies the number of bits to move. | The result is 15 and the binary is 0000 1111 |
The following example demonstrates the use of logical operators:
package main import "fmt" func main() { var a uint = 60 /* 60 = 0011 1100 */ var b uint = 13 /* 13 = 0000 1101 */ var c uint = 0 c = a & b /* 12 = 0000 1100 */ fmt.Printf("第一行 - c 的值为 %d\n", c ) c = a | b /* 61 = 0011 1101 */ fmt.Printf("第二行 - c 的值为 %d\n", c ) c = a ^ b /* 49 = 0011 0001 */ fmt.Printf("第三行 - c 的值为 %d\n", c ) c = a << 2 /* 240 = 1111 0000 */ fmt.Printf("第四行 - c 的值为 %d\n", c ) c = a >> 2 /* 15 = 0000 1111 */ fmt.Printf("第五行 - c 的值为 %d\n", c ) }
The results of the above instance run:
第一行 - c 的值为 12 第二行 - c 的值为 61 第三行 - c 的值为 49 第四行 - c 的值为 240 第五行 - c 的值为 15
The following table lists assignment operators for all Go languages.
Operator | Describe | Instance |
---|---|---|
= | A simple assignment operator that assigns the value of an expression to a left value | The result of the A-B expression is assigned to C |
+= | Add it up and assign it | C s A is equal to C s C s A |
-= | Subtract and assign | C - - A equals C - C - A |
*= | Multiply and assign | C |
/= | Divide and assign | C /-A is equal to C-C/A |
%= | Ask for the rest before assigning the value | C %-A is equal to C-C %A |
<<= | Shift left to assign a value | C slt;lt;2 is equal to C-lt;lt;2 |
>>= | The assignment is made after the right shift | C is equal to C and C, and 2 is equal to C |
&= | Assign by bit and back | C and 2 are equal to C and C and 2 |
^= | Assign values by bit difference or after | C s 2 is equal to C s C s 2 |
|= | Assign by bit or back | C | is equal to C is equal to C | 2 |
The following example demonstrates the use of assignment operators:
package main import "fmt" func main() { var a int = 21 var c int c = a fmt.Printf("第 1 行 - = 运算符实例,c 值为 = %d\n", c ) c += a fmt.Printf("第 2 行 - += 运算符实例,c 值为 = %d\n", c ) c -= a fmt.Printf("第 3 行 - -= 运算符实例,c 值为 = %d\n", c ) c *= a fmt.Printf("第 4 行 - *= 运算符实例,c 值为 = %d\n", c ) c /= a fmt.Printf("第 5 行 - /= 运算符实例,c 值为 = %d\n", c ) c = 200; c <<= 2 fmt.Printf("第 6行 - <<= 运算符实例,c 值为 = %d\n", c ) c >>= 2 fmt.Printf("第 7 行 - >>= 运算符实例,c 值为 = %d\n", c ) c &= 2 fmt.Printf("第 8 行 - &= 运算符实例,c 值为 = %d\n", c ) c ^= 2 fmt.Printf("第 9 行 - ^= 运算符实例,c 值为 = %d\n", c ) c |= 2 fmt.Printf("第 10 行 - |= 运算符实例,c 值为 = %d\n", c ) }
The results of the above instance run:
第 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
The following table lists other operators in the Go language.
Operator | Describe | Instance |
---|---|---|
& | Returns the variable storage address | &a; The actual address of the variable is given. |
* | The pointer variable. | *a; is a pointer variable |
The following example demonstrates the use of other operators:
package main import "fmt" func main() { var a int = 4 var b int32 var c float32 var ptr *int /* 运算符实例 */ fmt.Printf("第 1 行 - a 变量类型为 = %T\n", a ); fmt.Printf("第 2 行 - b 变量类型为 = %T\n", b ); fmt.Printf("第 3 行 - c 变量类型为 = %T\n", c ); /* & 和 * 运算符实例 */ ptr = &a /* 'ptr' 包含了 'a' 变量的地址 */ fmt.Printf("a 的值为 %d\n", a); fmt.Printf("*ptr 为 %d\n", *ptr); }
The results of the above instance run:
第 1 行 - a 变量类型为 = int 第 2 行 - b 变量类型为 = int32 第 3 行 - c 变量类型为 = float32 a 的值为 4 *ptr 为 4
Some operators have a higher priority, and binary operators are moved from left to right. The following table lists all operators and their priorities, representing the highest to lowest priorities from top to bottom:
Priority | Operator |
---|---|
7 | ^ ! |
6 | * / % <> & &^ |
5 | + - | ^ |
4 | == != < = > |
3 | <- |
2 | && |
1 | || |
Of course, you can temporarily elevate the overall operation priority of an expression by using parentheses.
The results of the above instance run:
package main import "fmt" func main() { var a int = 20 var b int = 10 var c int = 15 var d int = 5 var e int; e = (a + b) * c / d; // ( 30 * 15 ) / 5 fmt.Printf("(a + b) * c / d 的值为 : %d\n", e ); e = ((a + b) * c) / d; // (30 * 15 ) / 5 fmt.Printf("((a + b) * c) / d 的值为 : %d\n" , e ); e = (a + b) * (c / d); // (30) * (15/5) fmt.Printf("(a + b) * (c / d) 的值为 : %d\n", e ); e = a + (b * c) / d; // 20 + (150/5) fmt.Printf("a + (b * c) / d 的值为 : %d\n" , e ); }
The results of the above instance run:
(a + b) * c / d 的值为 : 90 ((a + b) * c) / d 的值为 : 90 (a + b) * (c / d) 的值为 : 90 a + (b * c) / d 的值为 : 50