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.

##%Find remainderB % A Output result 0++incrementA++ output result 11--自Minus A--Output result 9
OperatorDescriptionInstance
+ AdditionA + B output result 30
-SubtractionA - B output result-10
*MultiplyA * B Output result 200
/ DivisionB / A Output result 2
The following examples demonstrate the usage of each arithmetic operator:

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 operators

The 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.

OperatorDescriptionInstance==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

The following examples demonstrate the usage of relational 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 above example running results:

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

Logical operators

The following table lists all Logical operators in Go language. Assume that A has a value of True and B has a value of False.

OperatorDescriptionInstance
&& 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 ^:

##pqp & qp | qp ^ q000 00010111111010011

Assume A = 60; B = 13; Its 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 C language are as shown in the following table. Suppose A is 60 and B is 13:

##^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##>>

The following examples demonstrate the usage 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 above example execution results:

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

Assignment operators

The following table lists all Assignment operator in Go language.

OperatorDescriptionInstance
&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
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
##> >=Assign value after right shiftC >>= 2 is equal to C = C >> 2##&=^=##|=Bitwise OR post-assignmentC |= 2 is equal to C = C | 2The following example demonstrates the usage of the assignment operator:
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 )

}
OperatorDescriptionInstance
= Simple assignment operator, assigns the value of an expression to an lvalueC = A + B assigns the result of the A + B expression to C
+ =After addition, assign valueC += 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 valueC /= A is equal to C = C / A
%=Find the remainder and then assign the valueC %= A equals C = C % A
< ;<=Assign value after left shiftC <<= 2 is equal to C = C << 2
Assignment after bitwise ANDC &= 2 is equal to C = C & 2
Bitwise XOR Post-assignmentC ^= 2 is equal to C = C ^ 2
The above example running result:

第 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.

OperatorDescriptionInstance& Returning the variable storage address &a; will give the actual address of the variable. *Pointer variable. *a; is a pointer variableThe following examples demonstrate the usage 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 above example running results:

第 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:

PriorityOperation Symbol7^ !6* / % << > > & &^5+ - | ^4== ! = < <= >= ><-&&##1
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 above instance running results:
(a + b) * c / d 的值为 : 90
((a + b) * c) / d 的值为  : 90
(a + b) * (c / d) 的值为  : 90
a + (b * c) / d 的值为  : 50
##3
2
##||
## Of course, you can temporarily boost something by using parentheses The overall evaluation precedence of an expression. The above instance running results: