English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Operators are the foundation of any programming language. Therefore, the functionality of Go language is incomplete without using operators. Operators allow us to perform different types of operations on operands. In Go language, operators can be classified according to their different functions:
These are used to perform arithmetic operations on operands in Go language/Mathematical operations:
Addition: " +The 'add' operator adds two operands. For example, x + y.
Subtraction: "-The 'sub' operator subtracts two operands. For example, x-y.
Multiplication: div*The 'mul' operator multiplies two operands. For example, x * y.
Division: div/The 'div' operator divides the first operand by the second. For example, x / y.
Modulo:When the first operand is divided by the second operand, the '%' operator returns the remainder. For example, x%y.
Note: -,+, !, &*, <-The 'and' operator is also known as a unary operator, and unary operators have higher precedence.++The 'and' operator comes from the statement, it is not an expression, so it is not in the operator hierarchy.
package main import "fmt" func main() { p := 34 q := 20 // Addition result1 := p + q fmt.Printf("Calculation result p + q = %d", result1) // Subtraction result2 := p - q fmt.Printf("\nCalculation result p - q = %d", result2) // Multiplication result3 := p * q fmt.Printf("\nCalculation result p * q = %d", result3) // Division result4 := p / q fmt.Printf("\nCalculation result p / q = %d", result4) // Modulo result5 := p % q fmt.Printf("\nCalculation result p %% q = %d", result5) }
Output:
The result of p is calculated as follows: + q = 54 The result of p is calculated as follows: - q = 14 The result of p is calculated as follows: * q = 680 The result of p is calculated as follows: / q = 1 The result of p % q is calculated as follows: 14
Relational operators are used to compare two values. Let's take a look at them one by one:
'==' (equal to)The operator checks if two given operands are equal. If equal, it returns true. Otherwise, it returns false. For example,5 == 5will return true.
'!=' (not equal to)The operator checks if two given operands are equal. If not equal, it returns true. Otherwise, it returns false. It is the exact boolean complement of the '==' operator. For example,5!= 5will return false.
'>' (greater than)The operator checks if the first operand is greater than the second operand. If greater, it returns true. Otherwise, it returns false. For example,6> 5will return true.
'<' (less than)The operator checks if the first operand is less than the second operand. If less, it returns true. Otherwise, it returns false. For example,6 <5will return false.
'>=' (greater than or equal to)The operator checks if the first operand is greater than or equal to the second operand. If greater than or equal, it returns true. Otherwise, it returns false. For example,5> = 5will return true.
" <=" (less than or equal to)The operator checks if the first operand is less than or equal to the second operand. If less than or equal, it returns true. Otherwise, it returns false. For example,5 <= 5will also return true.
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 or equal to) result5 := p >= q fmt.Println(result5) // '<=' (less than or equal to) result6 := p <= q fmt.Println(result6) }
Output:
false true false true true false
They are used to combine two or more conditions/Constraint, or additional consideration of the original condition's evaluation.
Logical AND:The "&&" operator returns true when both of the considered conditions are met. Otherwise, it returns false. For example, when both a and b are true (i.e., non-zero), a && b returns true.
Logical OR: The "||" operator returns true when one (or both) of the conditions is met. Otherwise, it returns false. For example, if either a or b is true (i.e., non-zero), || b returns true. Of course, when both a and b are true, it returns true.
Logical NOT: If the condition is not met, the "!" operator returns true. Otherwise, it returns false. For example, if a is false, i.e., a = 0, then !a returns true.
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") } }
Output:
True True True
In Go language, there are6bitwise operators can work bitwise or be used for bit-by-bit operations. The following are bitwise operators:
&(bitwise AND): TakeTwo numbers as operands, and perform 'and' operation on each bit of the two numbers. Only when both bits of1when, the AND result is1.
| (bitwise OR): TakeTwo numbers as operands, and perform 'or' operation on each bit of the two numbers. If any bit of the two bits is1,OR result is1.
^(bitwise XOR): TakeTwo numbers as operands, and perform XOR on each bit of the two numbers. If the two bits are different, the XOR result is1.
<<(left shift):Take two numbers, left shift the bits of the first operand, and the second operand determines the number of bits to shift.
>>(right shift):Take two numbers, right shift the bits of the first operand, and the second operand determines the number of bits to shift.
&^(AND NOT):The bitwise clear operator, which actually performs the &(^) operation.
package main import "fmt" func main() { p := 134 q := 320 // & (AND) result1 := p & q fmt.Printf("The calculation result p & q = %d", result1) // | (OR) result2 := p | q fmt.Printf("\nThe calculation result p | q = %d", result2) // ^ (XOR) result3 := p ^ q fmt.Printf("\nThe calculation result p ^ q = %d", result3) // << (left shift) result4 := p << 1 fmt.Printf("\nThe calculation result p << 1 = %d", result4) // >> (right shift) result5 := p >> 1 fmt.Printf("\nThe calculation result p >> 1 = %d", result5) // &^ (AND NOT) result6 := p &^ q fmt.Printf("\nThe calculation result p &^ q = %d", result6) }
Output:
The calculation result p & q = 0 The calculation result p | q = 454 The calculation result p ^ q = 454 The calculation result p << 1 = 268 The calculation result p >> 1 = 67 The calculation result p &^ q = 134
The assignment operator is used to assign values to variables. The left operand of the assignment operator is a variable, while the right operand is a value. The value on the right must be of the same data type as the left variable, otherwise the compiler will throw an error. The different types of assignment operators are as follows:
" = "(simple assignment):This is the simplest assignment operator. It is used to assign the value on the right to the left variable.
" + " = "(addition assignment):This operator is+The combination of the '+' and '=' operators. The operator first adds the current value of the left variable to the value on the right, and then assigns the result to the left variable.
"-" = "(subtraction assignment):This operator is-The combination of the '-' and '=' operators. The operator first subtracts the current value of the left variable from the value on the right, and then assigns the result to the left variable.
" * " = "(multiplication assignment):This operator is*The combination of the '*' and '=' operators. The operator first multiplies the current value of the left variable by the value on the right, and then assigns the result to the left variable.
" / " = "(division assignment):This operator is/The combination of the '%' and '=' operators. The operator first divides the current value of the left variable by the value on the right, and then assigns the result to the left variable.
"% = "(modulus assignment):This operator is a combination of the '%' and '=' operators. The operator first multiplies the current value of the left variable by the value of the right variable, and then assigns the result to the left variable.
"& = "(bitwise AND assignment):This operator is a combination of the '&' and '=' operators. The operator first performs a bitwise AND operation between the current value of the left variable and the right variable, and then assigns the result to the left variable.
"^ = "(bitwise XOR):This operator is a combination of the '^' and '=' operators. The operator first performs a bitwise XOR operation between the current value of the left variable and the right variable, and then assigns the result to the left variable.
"| = "(bitwise OR):This operator is a combination of the "|" and '=' operators. The operator first performs a bitwise OR operation between the current value of the left variable and the value on the right, and then assigns the result to the left variable.
package main import "fmt" func main() { var p int = 38 var q int = 70 // " = "(simple assignment) p = q fmt.Println(p) // "+= (addition assignment) p += q fmt.Println(p) //"-= (subtraction assignment) p-= q fmt.Println(p) // "*= (multiplication assignment) p*= q fmt.Println(p) // "/= (division assignment) p /= q fmt.Println(p) // "%= "(modulus assignment) p %= q fmt.Println(p) }
Output:
70 140 70 4900 70 0
&:This operator returns the address of a variable.
*:This operator provides a pointer to a variable.
<-:The name of this operator is receive. It is used to receive values from a channel.
package main import "fmt" func main() { a := 94 //using operator (&) and //pointer indirect (*)operator b := &a fmt.Println(*b) *b = 67 fmt.Println(a) }
Output:
94 67