[Go] Go Language Tutorial--GO Language Operators (7)

Past tutorials:

Operators are used to perform mathematical or logical operations while the program is running.

The built-in operators in Go language are:

  • arithmetic operator
  • relational operator
  • Logical Operators
  • bitwise operator
  • assignment operator
  • other operators

Next, let's look at the introduction of each operator in detail.

arithmetic operator

The following table lists all the arithmetic operators of Go language. Assume A has a value of 10 and B has a value of 20.

operator describe example
+ add up A + B output result 30
- Subtract A - B output result -10
* multiplied A * B output result 200
/ to divide B / A output result 2
% Surplus B % A output result 0
++ self-increment A++ output result 11
Decrement A-- output result 9

The following examples demonstrate the use of the various arithmetic operators:

example

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("第六行 - a 的值为 %d\n", a )
   a=21   // 为了方便测试,a 这里重新赋值为 21
   a--
   fmt.Printf("第七行 - a 的值为 %d\n", a )
}

The result of running the above example:

第一行 - c 的值为 31
第二行 - c 的值为 11
第三行 - c 的值为 210
第四行 - c 的值为 2
第五行 - c 的值为 1
第六行 - a 的值为 22
第七行 - a 的值为 20

relational operator

The following table lists all relational operators in Go language. Assume A has a value of 10 and B has a value of 20.

operator describe example
== Checks if two values ​​are equal and returns True if they are equal, False otherwise. (A == B) is False
!= Checks if two values ​​are not equal and returns True if not, False otherwise. (A != B) is True
> Checks whether the value on the left is greater than the value on the right, and returns True if it is, False otherwise. (A > B) is False
< Checks whether the value on the left is less than the value on the right, and returns True if it is, False otherwise. (A < B) is True
>= Checks whether the value on the left is greater than or equal to the value on the right, and returns True if it is, otherwise returns False. (A >= B) is False
<= Checks whether the value on the left is less than or equal to the value on the right, and returns True if it is, otherwise returns False. (A <= B) is True

The following example demonstrates the usage of relational operators:

example

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 大于等于 a\n" )
   }
}

The result of running the above example:

第一行 - a 不等于 b
第二行 - a 不小于 b
第三行 - a 大于 b
第四行 - a 小于等于 b
第五行 - b 大于等于 a

Logical Operators

The following table lists all logical operators in Go language. Assume the value of A is True and the value of B is False.

operator describe example
&& Logical AND operator. The condition is True if both operands are True, otherwise it is False. (A && B) One 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. The logical NOT condition is False if the condition is True, otherwise it is True. !(A && B) One True

The following example demonstrates the use of logical operators:

example

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 result of running the above example:

第二行 - 条件为 true
第三行 - 条件为 false
第四行 - 条件为 true

bitwise operator

Bitwise operators operate on the binary bits of integers in memory.

The following table lists the evaluation of the bitwise operators &, |, and ^:

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 = 60; B = 13; its binary number conversion is:

A = 0011 1100

B = 0000 1101

-----------------

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

The bitwise operators supported by the Go language are listed in the table below. Assume A is 60 and B is 13:

operator describe example
& The bitwise AND operator "&" is a binary operator. Its function is the binary phase AND of the two numbers involved in the operation. (A & B) The result is 12, which is 0000 1100 in binary
| The bitwise OR operator "|" is a binary operator. Its function is to participate in the operation of the two corresponding binary phase or (A|B) The result is 61, binary is 0011 1101
^ The bitwise XOR operator "^" is a binary operator. Its function is to participate in the operation of two numbers corresponding to the binary difference or, when the two corresponding binary differences, the result is 1. (A ^ B) results in 49, which is 0011 0001 in binary
<< The left shift operator "<<" is a binary operator. Shifting left by n bits is multiplying by 2 to the nth power. Its function shifts all the binary bits of the operand on the left of "<<" to the left by several bits, and the number on the right of "<<" specifies the number of digits to be shifted, the high bits are discarded, and the low bits are filled with 0. A << 2 results in 240, which is 1111 0000 in binary
>> The right shift operator ">>" is a binary operator. Shifting right by n bits is dividing by 2 to the nth power. Its function is to shift all the binary bits of the operand on the left of ">>" to the right by several bits, and the number on the right of ">>" specifies the number of bits to move. A >> 2 results in 15, which is 0000 1111 in binary

The following example demonstrates the usage of bitwise operators:

example

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 result of running the above example:

第一行 - c 的值为 12
第二行 - c 的值为 61
第三行 - c 的值为 49
第四行 - c 的值为 240
第五行 - c 的值为 15

assignment operator

The following table lists all assignment operators in Go language.

operator describe example
= Simple assignment operator that assigns the value of an expression to an lvalue C = A + B assigns the result of the expression A + B to C
+= Add and then assign C += A is equal to C = C + A
-= Subtract and then assign C -= A is equal to C = C - A
*= Multiply and assign C *= A is equal to C = C * A
/= assign after division C /= A is equal to C = C / A
%= assign after remainder C %= A is equal to C = C % A
<<= assignment after left shift C <<= 2 等于 C = C << 2
>>= assignment after right shift C >>= 2 is equal to C = C >> 2
&= bitwise and post-assignment C &= 2 is equal to C = C & 2
^= Assignment after bitwise XOR C ^= 2 is equal to C = C ^ 2
|= bitwise or post-assignment C|= 2 equals C = C

The following example demonstrates the usage of assignment operator:

example

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 result of running the above example:

1- =  运算符实例,c 值为 = 212- += 运算符实例,c 值为 = 423- -= 运算符实例,c 值为 = 214- *= 运算符实例,c 值为 = 4415- /= 运算符实例,c 值为 = 216- <<= 运算符实例,c 值为 = 8007- >>= 运算符实例,c 值为 = 2008- &= 运算符实例,c 值为 = 09- ^= 运算符实例,c 值为 = 210- |= 运算符实例,c 值为 = 2

other operators

The following table lists other operators in Go language.

operator describe example
& Return variable storage address &a; will give the actual address of the variable.
* pointer variable. *a; is a pointer variable

The following examples demonstrate the use of other operators:

example

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 result of running the above example:

1- a 变量类型为 = int2- b 变量类型为 = int323- c 变量类型为 = float32
a 的值为  4
*ptr 为 4

operator precedence

Some operators have higher precedence, and binary operators operate from left to right. The following table lists all operators and their precedence, from top to bottom represents the priority from high to low:

priority operator
5 * / % << >> & &^
4 + - | ^
3 == != < <= > >=
2 &&
1 ||

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

The result of running the above example:

example

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 result of running the above example:

(a + b) * c / d 的值为 : 90
((a + b) * c) / d 的值为  : 90
(a + b) * (c / d) 的值为  : 90
a + (b * c) / d 的值为  : 50

Guess you like

Origin blog.csdn.net/u011397981/article/details/131567591