Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Go Language Operators

Go Language Operators

Operators are used to perform mathematical or logical operations at program runtime.

The built-in operators in Go are:

Next, let's take a detailed look at each operator.


Arithmetic operators

The following table lists all arithmetic operators in Go. Assume A = 10 and B = 20.

operator Description Example
+ Sum A + B outputs 30
- Subtract A - B outputs -10
* Multiply A * B outputs 200
/ Divide B / A outputs 2
% Modulo (Remainder) B % A outputs 0
++ Increment A++ outputs 11
-- Decrement A-- outputs 9

The following example demonstrates the use of each arithmetic operator:

Example

package

main

import

"fmt"

func

main

()

{

   

var

a

int

=

21

   

var

b

int

=

10

   

var

c

int

   c

=

a

b

   fmt

.

Printf

(

"First line - value of c is %d\n"

,

c

)

   c

=

a

b

   fmt

.

Printf

(

"Second line - value of c is %d\n"

,

c

)

   c

=

a

b

   fmt

.

Printf

(

"Third line - value of c is %d\n"

,

c

)

   c

=

a

/

b

   fmt

.

Printf

(

"Fourth line - value of c is %d\n"

,

c

)

   c

=

a

%

b

   fmt

.

Printf

(

"Fifth line - value of c is %d\n"

,

c

)

   a

++

   fmt

.

Printf

(

"Sixth line - value of a is %d\n"

,

a

)

   a

=

21

 

// For easier testing, a is reassigned to 21 here

   a

--

   fmt

.

Printf

(

"Seventh line - value of a is %d\n"

,

a

)

}

The result of the above example:



第一行 - c 的值为 31

第二行 - c 的值为 11

第三行 - c 的值为 210

第四行 - c 的值为 2

第五行 - c 的值为 1

第六行 - a 的值为 22

第七行 - a 的值为 20


Relational operators

The following table lists all relational operators in Go. Assume A = 10 and B = 20.

operator Description Example
== Checks whether two values are equal; returns True if they are, otherwise returns False. (A == B) is False
!= Checks whether two values are not equal; returns True if they are not, otherwise returns False. (A != B) is True
> Checks whether the left value is greater than the right value; returns True if so, otherwise returns False. (A > B) is False
< Checks whether the left value is less than the right value; returns True if so, otherwise returns False. (A < B) is True
>= Checks whether the left value is greater than or equal to the right value; returns True if so, otherwise returns False. (A >= B) is False
<= Checks whether the left value is less than or equal to the right value; returns True if so, otherwise returns False. (A <= B) is True

The following example demonstrates the use of relational operators:

Example

package

main

import

"fmt"

func

main

()

{

   

var

a

int

=

21

   

var

b

int

=

10

   

if

(

a

==

b

)

{

      fmt

.

Printf

(

"First line - a equals b\n"

)

   

}

else

{

      fmt

.

Printf

(

"First line - a does not equal b\n"

)

   

}

   

if

(

a < b

)

{

      fmt

.

Printf

(

"Second line - a is less than b\n"

)

   

}

else

{

      fmt

.

Printf

(

"Second line - a is not less than b\n"

)

   

}

   

   

if

(

a > b

)

{

      fmt

.

Printf

(

"Third line - a is greater than b\n"

)

   

}

else

{

      fmt

.

Printf

(

"Third line - a is not greater than b\n"

)

   

}

   

/* Lets change value of a and b */

   a

=

5

   b

=

20

   

if

(

a <

=

b

)

{

      fmt

.

Printf

(

"Fourth line - a is less than or equal to b\n"

)

   

}

   

if

(

b >

=

a

)

{

      fmt

.

Printf

(

"Fifth line - b is greater than or equal to a\n"

)

   

}

}

The result of the above example:



第一行 - a 不等于 b

第二行 - a 不小于 b

第三行 - a 大于 b

第四行 - a 小于等于 b

第五行 - b 大于等于 a


Logical operators

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

operator Description Example
&& Logical AND operator. If both operands are True, the condition is True; otherwise it is False. (A && B) is False
|| Logical OR operator. If either operand is True, the condition is True; otherwise it is False. (A || B) is True
! Logical NOT operator. If the condition is True, the logical NOT condition is False; otherwise it is True. !(A && B) is 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

(

"First line - condition is true\n"

)

   

}

   

if

(

a

||

b

)

{

      fmt

.

Printf

(

"Second line - condition is true\n"

)

   

}

   

/* Modify the values of a and b */

   a

=

false

   b

=

true

   

if

(

a && b

)

{

      fmt

.

Printf

(

"Third line - condition is true\n"

)

   

}

else

{

      fmt

.

Printf

(

"Third line - condition is false\n"

)

   

}

   

if

(

!

(

a && b

)

)

{

      fmt

.

Printf

(

"Fourth line - condition is true\n"

)

   

}

}

The result of the above example:



第二行 - 条件为 true

第三行 - 条件为 false

第四行 - 条件为 true


Bitwise operators

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

The following table lists the operations 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

Assume A = 60; B = 13; their binary representations are:



A = 0011 1100



B = 0000 1101



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



A&B = 0000 1100



A|B = 0011 1101



A^B = 0011 0001

The bitwise operators supported by Go are shown in the following table. Assume A is 60 and B is 13:

operator Description Example
& The bitwise AND operator "&" is a binary operator. Its function is to perform AND on the corresponding bits of the two operands. (A & B) results in 12, binary 0000 1100
| The bitwise OR operator "|" is a binary operator. Its function is to perform OR on the corresponding bits of the two operands (A | B) results in 61, binary 0011 1101
^ The bitwise XOR operator "^" is a binary operator. Its function is to perform XOR on the corresponding bits of the two operands; when the corresponding bits differ, the result is 1. (A ^ B) results in 49, binary 0011 0001
<< Left shift operator "<<" is a binary operator. Shifting left by n bits means multiplying by 2 to the power of n. Its function is to "<<" shift all the binary bits of the operand on the left by several places, as specified by "<<"; high-order bits are discarded and low-order bits are filled with 0. A << 2 results in 240, binary 1111 0000
>> The right shift operator ">>" is a binary operator. Shifting right by n bits means dividing by 2 to the power of n. Its function is to shift all the binary bits of the operand on the left of ">>" by several places to the right, with the number on the right of ">>" specifying the number of places to shift. A >> 2 results in 15, binary 0000 1111

The following example demonstrates the use 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

(

"First line - value of c is %d\n"

,

c

)

   c

=

a

|

b      

/* 61 = 0011 1101 */

   fmt

.

Printf

(

"Second line - value of c is %d\n"

,

c

)

   c

=

a

^

b      

/* 49 = 0011 0001 */

   fmt

.

Printf

(

"Third line - value of c is %d\n"

,

c

)

   c

=

a <<

2

   

/* 240 = 1111 0000 */

   fmt

.

Printf

(

"Fourth line - value of c is %d\n"

,

c

)

   c

=

a >>

2

   

/* 15 = 0000 1111 */

   fmt

.

Printf

(

"Fifth line - value of c is %d\n"

,

c

)

}

The result of the above example:



第一行 - c 的值为 12

第二行 - c 的值为 61

第三行 - c 的值为 49

第四行 - c 的值为 240

第五行 - c 的值为 15


Assignment operators

The following table lists all assignment operators in Go.

operator Description Example
= Simple assignment operator, which assigns the value of an expression to a left value C = A + B assigns the result of the expression A + B to C
+= Add and then assign C += A is equivalent to C = C + A
-= Subtract and assign C -= A is equivalent to C = C - A
*= Multiply and assign C *= A is equivalent to C = C * A
/= Divide and assign C /= A is equivalent to C = C / A
%= Modulo and assign C %= A is equivalent to C = C % A
<<= Left shift and assign C <<= 2 is equivalent to C = C << 2
>>= Right shift and assign C >>= 2 is equivalent to C = C >> 2
&= Bitwise AND and assign C &= 2 is equivalent to C = C & 2
^= Bitwise XOR and assign C ^= 2 is equivalent to C = C ^ 2
|= Bitwise OR and assign C |= 2 is equivalent to C = C | 2

The following example demonstrates the use of assignment operators:

Example

package

main

import

"fmt"

func

main

()

{

   

var

a

int

=

21

   

var

c

int

   c

=

 a

   fmt

.

Printf

(

"Line 1 - = operator example, c value is = %d\n"

,

c

)

   c

+=

 a

   fmt

.

Printf

(

"Line 2 - += operator example, c value is = %d\n"

,

c

)

   c

-=

 a

   fmt

.

Printf

(

"Line 3 - -= operator example, c value is = %d\n"

,

c

)

   c

*=

 a

   fmt

.

Printf

(

"Line 4 - *= operator example, c value is = %d\n"

,

c

)

   c

/=

 a

   fmt

.

Printf

(

"Line 5 - /= operator example, c value is = %d\n"

,

c

)

   c  

=

200

;

   c <<

=

 

2

   fmt

.

Printf

(

"Line 6 - <<= operator example, c value is = %d\n"

,

c

)

   c >>

=

 

2

   fmt

.

Printf

(

"Line 7 - >>= operator example, c value is = %d\n"

,

c

)

   c &

=

 

2

   fmt

.

Printf

(

"Line 8 - &= operator example, c value is = %d\n"

,

c

)

   c

^=

 

2

   fmt

.

Printf

(

"Line 9 - ^= operator example, c value is = %d\n"

,

c

)

   c

|=

 

2

   fmt

.

Printf

(

"Line 10 - |= operator example, c value is = %d\n"

,

c

)

}

The result of the above example:



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

operator Description Example
& Returns the address where a variable is stored &a; will give the actual address of the variable.
* Pointer variable. *a; is a pointer variable

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

   

/* Operator example */

   fmt

.

Printf

(

"Line 1 - type of variable a is = %T\n"

,

a

);

   fmt

.

Printf

(

"Line 2 - type of variable b is = %T\n"

,

b

);

   fmt

.

Printf

(

"Line 3 - type of variable c is = %T\n"

,

c

);

   

/* & and * operator example */

   ptr

=

&

a    

/* 'ptr' contains the address of variable 'a' */

   fmt

.

Printf

(

"value of a is %d\n"

,

a

);

   fmt

.

Printf

(

"*ptr is %d\n"

,

ptr

);

}

The result of the above example:



第 1 行 - a 变量类型为 = int

第 2 行 - b 变量类型为 = int32

第 3 行 - c 变量类型为 = float32

a 的值为  4

*ptr 为 4


Operator Precedence

Some operators have higher precedence, and binary operators are evaluated from left to right. The following table lists all operators and their precedence, from top to bottom representing highest to lowest precedence:

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

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

The result of 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

(

"value of (a + b) * c / d is : %d\n"

,

 e

);

   e

=

((

a

b

)

c

)

/

d

;

   

// (30 * 15 ) / 5

   fmt

.

Printf

(

"value of ((a + b) * c) / d is : %d\n"

,

 e

);

   e

=

(

a

b

)

(

c

/

d

);

 

// (30) * (15/5)

   fmt

.

Printf

(

"value of (a + b) * (c / d) is : %d\n"

,

 e

);

   e

=

a

(

b

c

)

/

d

;

   

//  20 + (150/5)

   fmt

.

Printf

(

"value of a + (b * c) / d is : %d\n"

,

 e

);

 

}

The result of the above example:



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

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

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

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