# Go - 运算符

• 算术运算符
• 关系运算符
• 逻辑运算符
• 按位运算符
• 赋值运算符
• 其他运算符

## 算术运算符

Operator说明Example
+ A + B=30
- A-B给出-10
* A * B给出200
/除。 B/A等于2

++将整数值加1。 A ++给出11
-将整数值减1。 A--给出9

```package main

import "fmt"

func main() {

var a int = 21
var b int = 10
var c int

c = a + b
fmt.Printf("Line 1 - Value of c is %d\n", c )

c = a - b
fmt.Printf("Line 2 - Value of c is %d\n", c )

c = a * b
fmt.Printf("Line 3 - Value of c is %d\n", c )

c = a / b
fmt.Printf("Line 4 - Value of c is %d\n", c )

c = a % b
fmt.Printf("Line 5 - Value of c is %d\n", c )

a++
fmt.Printf("Line 6 - Value of a is %d\n", a )

a--
fmt.Printf("Line 7 - Value of a is %d\n", a )
}```

```Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of a is 22
Line 7 - Value of a is 21```

## 关系运算符

Operator描述Example
==相等(A == B) is not true.
!=不相等(A != B) is true.
>大于(A > B) is not true.
<小于(A < B) is true.
>=大于或等于(A >= B) is not true.
<=小于或等于(A <= B) is true.

```package main

import "fmt"

func main() {
var a int = 21
var b int = 10

if( a == b ) {
fmt.Printf("Line 1 - a is equal to b\n" )
} else {
fmt.Printf("Line 1 - a is not equal to b\n" )
}
if ( a < b ) {
fmt.Printf("Line 2 - a is less than b\n" )
} else {
fmt.Printf("Line 2 - a is not less than b\n" )
}
if ( a > b ) {
fmt.Printf("Line 3 - a is greater than b\n" )
} else {
fmt.Printf("Line 3 - a is not greater than b\n" )
}

/* Lets change value of a and b */
a = 5
b = 20
if ( a <= b ) {
fmt.Printf("Line 4 - a is either less than or equal to  b\n" )
}
if ( b >= a ) {
fmt.Printf("Line 5 - b is either greater than  or equal to b\n" )
}
}```

```Line 1 - a is not equal to b
Line 2 - a is not less than b
Line 3 - a is greater than b
Line 4 - a is either less than or equal to  b
Line 5 - b is either greater than  or equal to b```

## 逻辑运算符

Operator描述Example
&&逻辑AND运算符。如果两个操作数都不为零,则条件为true。.(A && B) is false.
||逻辑或运算符。 如果两个操作数中的任何一个都不为零,则条件为true。(A || B) is true.
!逻辑非运算符。 用于反转其操作数的逻辑状态。 如果条件为真,则逻辑非运算符将为假。!(A && B) is true.

```package main

import "fmt"

func main() {
var a bool = true
var b bool = false
if ( a && b ) {
fmt.Printf("Line 1 - Condition is true\n" )
}
if ( a || b ) {
fmt.Printf("Line 2 - Condition is true\n" )
}

/* lets change the value of  a and b */
a = false
b = true
if ( a && b ) {
fmt.Printf("Line 3 - Condition is true\n" )
} else {
fmt.Printf("Line 3 - Condition is not true\n" )
}
if ( !(a && b) ) {
fmt.Printf("Line 4 - Condition is true\n" )
}
}```

```Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true```

## 按位运算符

pqp & qp | qp ^ q
00000
01011
11110
10011

```A=0011 1100

B=0000 1101

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

A&B=0000 1100

A | B=0011 1101

A ^ B=0011 0001

〜A=1100 0011```

```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("Line 1 - Value of c is %d\n", c )

c = a | b       /* 61=0011 1101 */
fmt.Printf("Line 2 - Value of c is %d\n", c )

c = a ^ b       /* 49=0011 0001 */
fmt.Printf("Line 3 - Value of c is %d\n", c )

c = a << 2     /* 240=1111 0000 */
fmt.Printf("Line 4 - Value of c is %d\n", c )

c = a >> 2     /* 15=0000 1111 */
fmt.Printf("Line 5 - Value of c is %d\n", c )
}```

```Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is 240
Line 5 - Value of c is 15```

## 赋值运算符

Operator描述Example
=赋值C=A + B will assign value of A + B into C
+=相加C += A is equivalent to C=C + A
-=相减C -= A is equivalent to C=C - A
*=相乘C *= A is equivalent to C=C * A
/=相除C /= A is equivalent to C=C/A
%=取模C %= A is equivalent to C=C % A
<<=左移C <<= 2 is same as C=C << 2
>>=右移C >>= 2 is same as C=C >> 2
&=按位AND赋值运算符C &= 2 is same as C=C & 2
^=按位异或赋值运算符C ^= 2 is same as C=C ^ 2
|=按位非运算符和赋值运算符C |= 2 is same as C=C | 2

```package main

import "fmt"

func main() {
var a int = 21
var c int

c =  a
fmt.Printf("Line 1 -= Operator Example, Value of c=%d\n", c )

c +=  a
fmt.Printf("Line 2 - += Operator Example, Value of c=%d\n", c )

c -=  a
fmt.Printf("Line 3 - -= Operator Example, Value of c=%d\n", c )

c *=  a
fmt.Printf("Line 4 - *= Operator Example, Value of c=%d\n", c )

c /=  a
fmt.Printf("Line 5 - /= Operator Example, Value of c=%d\n", c )

c  = 200;

c <<=  2
fmt.Printf("Line 6 - <<= Operator Example, Value of c=%d\n", c )

c >>=  2
fmt.Printf("Line 7 - >>= Operator Example, Value of c=%d\n", c )

c &=  2
fmt.Printf("Line 8 - &= Operator Example, Value of c=%d\n", c )

c ^=  2
fmt.Printf("Line 9 - ^= Operator Example, Value of c=%d\n", c )

c |=  2
fmt.Printf("Line 10 - |= Operator Example, Value of c=%d\n", c )
}```

```Line 1 -= Operator Example, Value of c=21
Line 2 - += Operator Example, Value of c=42
Line 3 - -= Operator Example, Value of c=21
Line 4 - *= Operator Example, Value of c=441
Line 5 - /= Operator Example, Value of c=21
Line 6 - <<= Operator Example, Value of c=800
Line 7 - >>= Operator Example, Value of c=200
Line 8 - &= Operator Example, Value of c=0
Line 9 - ^= Operator Example, Value of c=2
Line 10 - |= Operator Example, Value of c=2```

## 其他运算符

Go语言还支持其他一些重要的运算符，包括 sizeof 和?:。

Operator描述Example
&返回变量的地址。&a;提供变量的实际地址。
*指向变量的指针。*a;提供指向变量的指针。

```package main

import "fmt"

func main() {
var a int = 4
var b int32
var c float32
var ptr *int

/* example of type operator */
fmt.Printf("Line 1 - Type of variable a=%T\n", a );
fmt.Printf("Line 2 - Type of variable b=%T\n", b );
fmt.Printf("Line 3 - Type of variable c= %T\n", c );

/* example of & and * operators */
ptr = &a	/* 'ptr' now contains the address of 'a'*/
fmt.Printf("value of a is  %d\n", a);
fmt.Printf("*ptr is %d.\n", *ptr);
}```

```Line 1 - Type of variable a=int
Line 2 - Type of variable b=int32
Line 3 - Type of variable c= float32
value of a is  4
*ptr is 4.```

## 运算符优先级

CategoryOperatorAssociativity
Postfix() [] -> . ++ - -Left to right
Unary+ - ! ~ ++ - - (type)* & sizeofRight to left
Multiplicative*/%Left to right
Shift<< >>Left to right
Relational< <= > >=Left to right
Equality== !=Left to right
Bitwise AND&Left to right
Bitwise XOR^Left to right
Bitwise OR|Left to right
Logical AND&&Left to right
Logical OR||Left to right
Conditional?:Right to left
Assignment= += -= *= /= %=>>= <<= &= ^= |=Right to left
Comma,Left to right

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

```Value of (a + b) * c/d is : 90
Value of ((a + b) * c)/d is  : 90
Value of (a + b) * (c/d) is  : 90
Value of a + (b * c)/d is  : 50```