The operator of the Golang entry Series III Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Preface: Operator Precedence, which describes the order in which operations are performed when a computer operation evaluates an expression. Perform an operation with a higher priority first, and then perform a lower-priority operation.

One, arithmetic operators

The following table lists all the arithmetic operators for the go language. A value of 10,b is assumed to be 20.

operator                       description                           instances                                 &NBS P                        
+ Add A + B Outputs the result of A
- subtraction A-B output result -10
* multiply A * B output result A.
/ Division b/a output result 2
% seek redundancy B% A output 0
+ + self-increment a++ output result one
-- auto-subtract a--output 9

Second, relational operators

The following table lists the relational operators for all the go languages. A value of 10,b is assumed to be 20.

operator Description Example
== Checks whether two values are equal, or returns False if equal returns TRUE. (A = = B) is False
!= Check that two values are not equal and return False if they are not equal. (A! = B) is True
> Checks if the left value is greater than the right value, or False if it returns TRUE. (A > B) is False
< Checks if the left value is less than the right value and returns False if it is true. (A < B) is true
>= Checks if the left value is greater than or equal to the right value, or False if it returns TRUE. (A >= B) is False
<= Checks whether the left value is less than or equal to the right value, or returns False if it is true. (A <= B) is True

Third, logical operators

The following table lists the logical operators for all the go languages. Assume that the A value is a true,b value of False.

operator Description Example
&& The logical AND operator. If the operands on both sides are true, the condition is true, otherwise False. (a && B) is False
|| The logical OR operator. If the operands on both sides have a true, the condition is true, otherwise False. (A | | B) is True
! Logical NOT operator. If the condition is true, the logical not condition is False, otherwise true. ! (a && B) is True

Four, bitwise operators

The bitwise operator operates on an integer in-memory bits. Suppose A is 60,b to 13

operator Description Example
& The bitwise AND operator "&" is the binocular operator. Its function is to participate in the operation of the two number of the corresponding binary phase. (A & B) result is 12, binary is 0000 1100
| Bitwise OR operator ' | ' is the binocular operator. Its function is to participate in the operation of the two number of the corresponding binary phase or (A | B) The result is 61 and the binary is 0011 1101
^ The bitwise XOR operator "^" is the binocular operator. Its function is to participate in the operation of the two number of the corresponding binary dissimilarity or, when the two corresponding binary differences, the result is 1. (A ^ B) result is 49, binary is 0011 0001
<< The left-shift operator "<<" is the binocular operator. The n-bit left shift is multiplied by 2 of the n-th square. Its function to the left of the "<<" operand of all the binary all left a number of bits, the "<<" to the right of the number to specify the number of bits moved, high drop, low 0. A << 2 result is 240, binary is 1111 0000
>> The right-shift operator ">>" is the binocular operator. The right-shift n-bit is the n-th-squared divided by 2. The function is to shift all the binary of the left operand of ">>" to the right of several bits, and the number to the right of ">>" to specify the number of bits to move. A >> 2 result is 15, binary is 0000 1111

Demo Example:

package mainimport "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}

Left shift Right Shift operator example (implement Calculator storage unit):

package mainimport "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 = 1024KB1GB = 1.048576e+06KB1TB = 1.073741824e+09KB1PB = 1.099511627776e+12KB

Five, assignment operators

The following table lists the assignment operators for all the go languages. Suppose A is 21

operator Description Example
= A simple assignment operator that assigns the value of an expression to a left-hand value c = A assigns a value to C, result: 21
+= Add and then assign value c + = a equals c = C + A, results: 42
-= Subtract and then assign a value c-= A equals c = c-a, Result: 21
*= Multiply and then assign values C *= a equals c = c * A, results: 441
/= Assign values after dividing C/= A equals c = c/a, Result: 21
%= After the remainder of the assignment value C%= a equals c = c% A, result: 0//not counted
<<= Assign value after left shift C <<= 2 equals c = C << 2, Result: 84
>>= Assign value after right shift C >>= 2 equals c = C >> 2, Result: 21
&= Bitwise AND post-assigned values C &= 2 equals c = C & 2, Result: 0
^= Bitwise XOR or post-assignment C ^= 2 equals c = c ^ 2, Result: 2
|= Bitwise OR post-assigned value C |= 2 equals c = C | 2, Results: 2

Vi. Other operators

operator Description Example
& Return variable store address &a; The actual address of the variable is given.
* Pointer variable. *a; is a pointer variable

Example of memory address and pointer: Print variable type with%t

package mainimport "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

Some operators have higher precedence, and the two-tuple operator is left-to-right in the direction of operation. The following table lists all operators and their precedence, from top to bottom, from highest to lowest:

优先级     运算符 7      ^ ! 6      * / % << >> & &^ 5      + - | ^ 4      == != < <= >= > 3      <- 2      && 1      ||

Of course, you can use parentheses to temporarily elevate the overall operation priority of an expression.

Reference: http://www.runoob.com/go/go-operators.html

In order to facilitate communication, I opened the public number (attention to see more wonderful) and QQ Group, QQ Group 1 (291519319) and QQ Group 2 (659336691). Like the technology to communicate with it

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.