这些用于在 Go 语言中对操作数执行算术/数学运算:
注意: -、+、!、&、*、<-、^ 也称为一元运算符,一元运算符的优先级较高。++ 和 — 运算符来自语句,它们不是表达式,因此它们不属于运算符层次结构。
例子;
// Go program to illustrate the // use of arithmetic operators package main import "fmt" func main() { p:= 34 q:= 20 // Addition result1:= p + q fmt.Printf("Result of p + q = %d", result1) // Subtraction result2:= p - q fmt.Printf("\nResult of p - q = %d", result2) // Multiplication result3:= p * q fmt.Printf("\nResult of p * q = %d", result3) // Division result4:= p / q fmt.Printf("\nResult of p / q = %d", result4) // Modulus result5:= p % q fmt.Printf("\nResult of p %% q = %d", result5) }
输出:
Result of p - q = 14 Result of p * q = 680 Result of p / q = 1 Result of p % q = 14
关系运算符用于比较两个值。让我们一一看看:
例子:
// Go program to illustrate the // use of relational operators package main import "fmt" func main() { p:= 34 q:= 20 // ‘=='(Equal To) result1:= p == q fmt.Println(result1) // ‘!='(Not Equal To) result2:= p != q fmt.Println(result2) // ‘<‘(Less Than) result3:= p < q fmt.Println(result3) // ‘>'(Greater Than) result4:= p > q fmt.Println(result4) // ‘>='(Greater Than Equal To) result5:= p >= q fmt.Println(result5) // ‘<='(Less Than Equal To) result6:= p <= q fmt.Println(result6) }
false true false true true false
它们用于组合两个或多个条件/约束,或补充所考虑的原始条件的评估。
// Go program to illustrate the // use of logical operators package main import "fmt" func main() { var p int = 23 var q int = 60 if(p!=q && p<=q){ fmt.Println("True") } if(p!=q || p<=q){ fmt.Println("True") } if(!(p==q)){ fmt.Println("True") } }
True True True
在Go语言中,有6个按位运算符,它们工作在位级别或用于执行逐位运算。以下是按位运算符:
// Go program to illustrate the // use of bitwise operators package main import "fmt" func main() { p:= 34 q:= 20 // & (bitwise AND) result1:= p & q fmt.Printf("Result of p & q = %d", result1) // | (bitwise OR) result2:= p | q fmt.Printf("\nResult of p | q = %d", result2) // ^ (bitwise XOR) result3:= p ^ q fmt.Printf("\nResult of p ^ q = %d", result3) // << (left shift) result4:= p << 1 fmt.Printf("\nResult of p << 1 = %d", result4) // >> (right shift) result5:= p >> 1 fmt.Printf("\nResult of p >> 1 = %d", result5) // &^ (AND NOT) result6:= p &^ q fmt.Printf("\nResult of p &^ q = %d", result6) }
Result of p & q = 0 Result of p | q = 54 Result of p ^ q = 54 Result of p << 1 = 68 Result of p >> 1 = 17 Result of p &^ q = 34
赋值运算符用于为变量赋值。赋值运算符的左侧操作数是变量,赋值运算符的右侧操作数是值。右侧的值必须与左侧的变量具有相同的数据类型,否则编译器将引发错误。不同类型的赋值运算符如下所示:
// Go program to illustrate the // use of assignment operators package main import "fmt" func main() { var p int = 45 var q int = 50 // “=”(Simple Assignment) p = q fmt.Println(p) // “+=”(Add Assignment) p += q fmt.Println(p) //“-=”(Subtract Assignment) p-=q fmt.Println(p) // “*=”(Multiply Assignment) p*= q fmt.Println(p) // “/=”(Division Assignment) p /= q fmt.Println(p) // “%=”(Modulus Assignment) p %= q fmt.Println(p) }
50 100 50 2500 50 0
// Go program to illustrate the // use of Misc Operators package main import "fmt" func main() { a := 4 // Using address of operator(&) and // pointer indirection(*) operator b := &a fmt.Println(*b) *b = 7 fmt.Println(a) }
4 7
原文链接:codingdict.net