Introduction: operator precedence, it is describing a sequential operation executed in the calculation expression of computing. Performing first operations with a higher priority, then the execution of low priority operations.
First, arithmetic operators
The following table lists all arithmetic operators Go language. Assuming that A is 10, B is 20.
Operators | description | Examples |
---|---|---|
+ | Adding | A + B output 30 |
- | Subtraction | A - B output -10 |
* | Multiplied | A * B output 200 |
/ | Divided | B / A output 2 |
% | Remainder | B% A 0 output |
++ | Increment | The output 11 A ++ |
-- | Decrement | A-- output 9 |
Second, relational operators
The following table lists the relational operators Go all languages. Assuming that A is 10, B is 20.
Operators | description | Examples |
---|---|---|
== | Check the two values are equal, equal returns True if otherwise False. | (A == B) to False |
!= | Check the two values are not equal, not equal returns True if otherwise False. | (A! = B) is True |
> | Check the left value is greater than the right value, it returns True if it is otherwise False. | (A> B) is False |
< | Check the left value is less than the right value, it returns True if it is otherwise False. | (A < B) 为 True |
>= | Check the left value is greater than or equal to the right value, it returns True if it is otherwise False. | (A> = B) to False |
<= | Check the left value is less than or equal to the right value, it returns True if it is otherwise False. | (A <= B) 为 True |
Third, the logical operators
The following table lists all logical operators in Go. Assuming that A is True, B is False.
Operators | description | Examples |
---|---|---|
&& | Logical AND operator. If both sides of the operands are True, the conditions are True, otherwise False. | (A && B) 为 False |
|| | Logical OR operator. If the number of operations on both sides of a True, the conditions are True, otherwise False. | (A || B) is True |
! | Logical NOT operator. If the condition is True, then the logical NOT condition is False, otherwise True. | !(A && B) 为 True |
Fourth, bitwise operators
Bitwise operators of integer bits in the memory operation. Assume that A is 60, B 13
Operators | description | Examples |
---|---|---|
& | Bitwise AND operator "&" is a binary operator. Its function is involved in computing the phase of two binary numbers each corresponding to the. | (A & B) as a result of 12, is binary 0000 1100 |
| | Bitwise OR operator "|" is the binary operators. Its function is involved in computing two binary numbers each corresponding phase or | (A | B) the result is 61, is binary 00111101 |
^ | Bitwise exclusive OR operator "^" is a binary operators. Its function is involved in computing the number of each of the two corresponding binary or different, when two dissimilar corresponding binary, the result is 1. | (A ^ B) the result is 49, is binary 00110001 |
<< | Left shift operator "<<" are binary operators. N-bit left shift is multiplied by 2 ^ n. The function of each of its binary operand "<<" left a number of bits to the left of all, the number of bits of a mobile "<<" specifies the right, discarding the upper, lower 0s. | A << 2 result 240 binary to 11110000 |
>> | Right shift operator ">>" are binary operators. Right by n bits is divided by 2 ^ n. Its function is to put all the various binary operand is ">>" Several left to the right, ">>" the number of bits specified by the right. | A >> 2 result 15, is binary 0000 1111 |
Demo:
package main
import "fmt"
func main() {
var a int = 60 //二进制是:111100
var b int = 13 //二进制是:001101
fmt.Printf("%b\n%d\n",a&b,a&b) //二进制是:1100,对应的十进制是12。说明&进行的是上下对应位的与操作
fmt.Printf("%b\n%d\n",a|b,a|b) //二进制是:111101,对应的十进制是61。说明&进行的是上下对应位的或操作
fmt.Printf("%b\n%d\n",a^b,a^b) //二进制是:110001,对应的十进制是49。^位运算符是上下对应位不同时,值为1
}
Examples of operators left right (to achieve the calculator memory unit):
package main
import "fmt"
const (
KB float64 = 1<<(10*iota) //iota是 const 结构里面,定义常量行数的索引器,每个 const 里面,iota 都从 0 开始
MB //下面是一个省略调用,继承了上面的表达式
GB
TB
PB
)
func main() {
fmt.Printf("1MB = %vKB\n",MB)
fmt.Printf("1GB = %vKB\n",GB)
fmt.Printf("1TB = %vKB\n",TB)
fmt.Printf("1PB = %vKB\n",PB)
}
运行结果:
1MB = 1024KB
1GB = 1.048576e+06KB
1TB = 1.073741824e+09KB
1PB = 1.099511627776e+12KB
Fifth, the assignment operator
The following table lists the assignment operator Go all languages. A is assumed 21
Operators | description | Examples |
---|---|---|
= | Simple assignment operator, the value of the expression of a value assigned to a left | A C = A will be assigned to C, results: 21 |
+= | Added together before assignment | C + = A is equal to C = C + A, results: 42 |
-= | After subtraction assignment | C - = A is equal to C = C - A, results: 21 |
*= | By multiplying assignment | C * = A is equal to C = C * A, results: 441 |
/= | Division after assignment | C / = A is equal to C = C / A, results: 21 |
%= | Remainder after assignment | C% = A is equal to C = C% A, results: 0 // not recorded in the calculation |
<<= | Left after the assignment | C << = 2 equal to C = C << 2, results: 84 |
>>= | Right after the assignment | C >> = 2 equal to C = C >> 2, the results: 21 |
&= | Bitwise and after the assignment | C & = 2 equal to C = C & 2, Results: 0 |
^= | After pressing bit XOR assignment | C ^ = 2 equal to C = C ^ 2, the results: 2 |
|= | After pressing position or assignment | C | = 2 equal to C = C | 2, the results: 2 |
Six other operators
Operators | description | Examples |
---|---|---|
& | Return variable memory address | & A; will be given of the actual address of the variable. |
* | Pointer variable. | * A; is a pointer variable |
Sample memory and address pointer:% T with print variable type
package main
import "fmt"
func main() {
var a int = 4
var b int32
var c float32
var ptr *int
fmt.Printf("a 变量类型为 = %T\n", a ) //输出变量类型%T
fmt.Printf("b 变量类型为 = %T\n", b )
fmt.Printf("c 变量类型为 = %T\n", c )
ptr = &a
fmt.Printf("a 的内存地址为 = %p",ptr) //go里面的内存块地址通常都是用十六进制表示的,因此输出:0x10414020a
fmt.Printf("*ptr 为 %d\n", *ptr) //这是个指向a的内存地址的指针,因此输出:4
}
Seven, operator precedence
有些运算符拥有较高的优先级,二元运算符的运算方向均是从左至右。下表列出了所有运算符以及它们的优先级,由上至下代表优先级由高到低:
优先级 运算符
7 ^ !
6 * / % << >> & &^
5 + - | ^
4 == != < <= >= >
3 <-
2 &&
1 ||
当然,你可以通过使用括号来临时提升某个表达式的整体运算优先级。
参考:http://www.runoob.com/go/go-operators.html