Latest web development tutorials

Go Language Operators

Operators are used to perform mathematical or logical operations in a running application.

Go operators are built into the language:

  • Arithmetic operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Other operators

Let's look at each in detail the operators introduced.


Arithmetic operators

The following table lists all languages ​​Go arithmetic operators. Assume that A is 10, B is 20.

Operators description Examples
+ Adding A + B output 30
- Subtraction A - B output -10
* Multiplied A * B 200 output
/ Divided B / A output 2
% Remainder B% A output 0
++ Increment A ++ output 11
- Decrement A-- output 9

The following example demonstrates the usage of various 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 )
}

Examples of the above operating results:

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

Relational Operators

The following table lists all of the Go language relational operators. Assume that A is 10, B is 20.

Operators description Examples
== Check the two values ​​are equal, equal returns True if otherwise False. (A == B) is False
! = Check whether 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 value of the right side, a return True otherwise False. (A> B) is False
< Check the left value is less than the value of the right side, a return True otherwise False. (A <B) is True
> = Check whether the value is greater than or equal to the left to the right value, it returns True if it is otherwise False. (A> = B) is False
<= Check whether the value is less than or equal to the left to the right value, it returns True if it is otherwise False. (A <= B) is True

The following example demonstrates 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" )
   }
}

Examples of the above operating results:

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

Logical Operators

The following table lists all languages ​​Go logical operators. Assume that A is True, B is False.

Operators description Examples
&& Logical AND operator. If both sides of the operands are True, the condition is True, otherwise False. (A && B) is False
|| Logical OR operator. If the operands on both sides there is a True, the condition is True, otherwise False. (A || B) is True
! Logical NOT operator. If the condition is True, then the logical NOT condition False, otherwise True. ! (A && 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" )
   }
}

Examples of the above operating results:

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

Bitwise Operators

Bitwise operators on integers in memory bits operate.

The following table lists the bitwise operators &, |, and ^ is calculated:

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

Assume that A = 60; B = 13; its binary number to:

A = 0011 1100

B = 0000 1101

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

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A  = 1100 0011

Bit computing support C language symbol shown in the following table. Assume that A is 60, B is 13:

Operators description Examples
& Bitwise AND operator "&" is the binary operators. Its function is two binary numbers corresponding to each phase of the operation and participation. (A & B) result is 12, the binary is 0000 1100
| Bitwise OR operator "|" is a binary operator. Its function is to participate in the operation of two numbers each corresponding binary phase or (A | B) the result is 61, the binary is 00111101
^ Bitwise exclusive OR operator "^" is the binary operators. Its function is to participate in the operation of two numbers each corresponding binary XOR when two corresponding binary bit different and the result is 1. (A ^ B) results for 49 binaries to 00,110,001
<< Left shift operator "<<" is the binary operators. N-bit left shift is multiplied by 2 ^ n. Its function to each binary "<<" left of the left operand of all a number of bits specified by the number "<<" right of the decimal moves, discarding the high and lower 0s. A << 2 results for 240 binary is 11110000
>> Right shift operator ">>" are binary operators. Right by n bits is divided by 2 ^ n. Its function is to various binary ">>" the left operand Several all right, ">>" on the right the number of bits specified. A >> 2 results for 15 0000 1111 binary system

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 )
}

Examples of the above operating results:

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

Assignment Operators

The following table lists all of the Go language assignment operator.

Operators description Examples
= Simple assignment operator assigns a value expression of an lvalue C = A + B A + B will be assigned to the C expression result
+ = Added together before assignment C + = A is equal to C = C + A
- = After subtraction assignment C - = A is equal to C = C - A
* = Then multiplying the assignment C * = A is equal to C = C * A
/ = Division after assignment C / = A is equal to C = C / A
% = Remainder after assignment C% = A is equal to C = C% A
<< = Left after assignment C << = 2 equal to C = C << 2
>> = Right after the assignment C >> = 2 equal to C = C >> 2
& = Bitwise AND assignment after C & = 2 equal to C = C & 2
^ = After pressing the bit XOR assignment C ^ = 2 equal to C = C ^ 2
| = After pressing position or assignment C | = 2 is equal to C = C | 2

The following example demonstrates the assignment operator usage:

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 )

}

Examples of the above operating results:

第 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 the other operators Go language.

Operators description Examples
& Return variable memory address & A; will give the real address of the variable.
* Pointer variable. * A; is a pointer variable

The following example demonstrates 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);
}

Examples of the above operating results:

第 1 行 - a 变量类型为 = int
第 2 行 - b 变量类型为 = int32
第 3 行 - c 变量类型为 = float32
a 的值为  4
*ptr 为 4

Operator Precedence

Some operators have a higher priority, binary operators are operational direction from left to right. The following table lists all the operators and their priorities, representatives from top to bottom in descending priority:

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

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

Examples of the above operating results:

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 );  
}

Examples of the above operating results:

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