Go - 运算符

Go - 运算符 首页 / Golang入门教程 / Go - 运算符

运算符是一个符号,告诉编译器执行特定的数学或逻辑操作。

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

算术运算符

下表显示了Go语言支持的所有算术运算符。假设变量 A=10,变量 B=0,然后-

无涯教程网

运算符描述示例
+ A + B=30
- A-B = -10
* A * B = 200
/除。 B/A = 2
取模运算符,给出整数除法后的余数。 B%A = 0
++将整数值加1。 A ++ = 11
-将整数值减1。 A-- = 9

尝试以下示例以了解Go编程语言中可用的所有算术运算符-

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

关系运算符

下表列出了Go语言支持的所有关系运算符。假设变量 A=10,变量 B=20,则-

运算符描述示例
==相等(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.

尝试以下示例以了解Go编程语言中可用的所有关系运算符-

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

逻辑运算符

下表列出了Go语言支持的所有逻辑运算符。假设变量 A=1,变量 B=0,则-

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

尝试以下示例以了解Go编程语言中可用的所有逻辑运算符-

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" )
   }
   
   /* 让我们改变 a 和 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=60;和B =13。以二进制格式,它们将如下所示-

A=0011 1100

B=0000 1101

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

A&B=0000 1100

A | B=0011 1101

A ^ B=0011 0001

A=1100 0011

尝试以下示例以了解Go编程语言中可用的所有按位运算符-

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

赋值运算符

下表列出了Go语言支持的所有赋值运算符-

运算符描述示例
=赋值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

尝试以下示例以了解Go编程语言中可用的所有赋值运算符-

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 和?:。

运算符描述示例
&返回变量的地址。&a;提供变量的实际地址。
*指向变量的指针。*a;提供指向变量的指针。

尝试以下示例以了解Go编程语言中可用的所有其他运算符-

package main

import "fmt"

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

   /* 类型运算符示例 */
   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 );

   /* & 和 * 运算符的示例 */
   ptr = &a	/* “ptr”现在包含“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
Additive+ -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

尝试以下示例以了解Go编程语言中可用的运算符优先级-

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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

趣谈Linux操作系统 -〔刘超〕

数据中台实战课 -〔郭忆〕

跟月影学可视化 -〔月影〕

重学线性代数 -〔朱维刚〕

To B市场品牌实战课 -〔曹林〕

动态规划面试宝典 -〔卢誉声〕

容器实战高手课 -〔李程远〕

手把手带你搭建推荐系统 -〔黄鸿波〕

AI 应用实战课 -〔黄佳〕

好记忆不如烂笔头。留下您的足迹吧 :)