Go Language Operators
Operators are used to perform mathematical or logical operations while the program is running.
The built-in operators in the Go language are:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Other Operators
Now let's take a detailed look at the introduction to each operator.
Arithmetic Operators
The following table lists all arithmetic operators in Go language. Assume A = 10, B = 20.
| Operators | Description | Example |
|---|---|---|
| + | Addition | A + B output result 30 |
| - | Subtraction | A - B output result -10 |
| * | Multiplication | A * B output result 200 |
| / | Division | B / A output result 2 |
| % | Remainder | B % A output result 0 |
| ++ | Increment | A++ output result 11 |
| -- | Decrement | A-- output result 9 |
The following examples demonstrate the usage of the arithmetic operators:
Example
import "fmt"
func main() {
var a int = 21
var b int = 10
var c int
c = a + b
fmt.Printf("Line 1 - the value of c is %d\n", c )
c = a - b
fmt.Printf("Line 2 - the value of c is %d\n", c )
c = a * b
fmt.Printf("Line 3 - the value of c is %d\n", c )
c = a / b
fmt.Printf("Line 4 - the value of c is %d\n", c )
c = a % b
fmt.Printf("Line 5 - the value of c is %d\n", c )
a++
fmt.Printf("Line 6 - the value of a is %d\n", a )
a=21 // For convenience of testing, a is reassigned to 21 here
a--
fmt.Printf("Line 7 - the value of a is %d\n", a )
}
Running 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 language. Assume A = 10, B = 20.
| Operators | Description | Example |
|---|---|---|
| == | Checks whether two values are equal. If they are equal, returns True; otherwise returns False. | (A == B) is False |
| != | Checks whether two values are not equal. If they are not equal, returns True; otherwise returns False. | (A != B) is True |
| > | Checks whether the value on the left is greater than the value on the right. If so, returns True; otherwise returns False. | (A > B) is False |
| < | Checks whether the value on the left is less than the value on the right. If so, returns True; otherwise returns False. | (A < B) is True |
| >= | Checks whether the value on the left is greater than or equal to the value on the right. If so, returns True; otherwise returns False. | (A >= B) is False |
| <= | Checks whether the value on the left is less than or equal to the value on the right. If so, returns True; otherwise returns False. | (A <= B) is True |
The following examples demonstrate the usage of the relational operators:
Example
import "fmt"
func main() {
var a int = 21
var b int = 10
if( a == b ) {
fmt.Printf("Line 1 - a equals 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 less than or equal to b\n" )
}
if ( b >= a ) {
fmt.Printf("Line 5 - b is greater than or equal to a\n" )
}
}
Running 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 language. Assume A = True, B = False.
| Operators | 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 examples demonstrate the usage of the logical operators:
Example
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" )
}
/* Modify the values 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 false\n" )
}
if ( !(a && b) ) {
fmt.Printf("Line 4 - condition is true\n" )
}
}
Running 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 calculations 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 |
Suppose A = 60; B = 13; their binary numbers are converted to:
A = 0011 1100 B = 0000 1101 ----------------- A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0001
The bitwise operators supported by Go language are shown in the following table. Assume A = 60, B = 13:
| Operators | Description | Example |
|---|---|---|
| & | The bitwise AND operator "&" is a binary operator. Its function is to perform a bitwise AND on the corresponding binary bits of the two numbers involved in the operation. | (A & B) results in 12, binary 0000 1100 |
| | | The bitwise OR operator "|" is a binary operator. Its function is to perform a bitwise OR on the corresponding binary bits of the two numbers involved in the operation. | (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 binary bits of the two numbers involved in the operation. When the corresponding binary bits are different, the result is 1. | (A ^ B) results in 49, binary 0011 0001 |
| << | The left shift operator "<<" is a binary operator. Left shifting by n bits is equivalent to multiplying by 2 to the nth power. Its function is to shift all binary bits of the operand on the left of "<<" to the left by a number of bits specified by the operand on the right of "<<"; 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. Right shifting by n bits is equivalent to dividing by 2 to the nth power. Its function is to shift all binary bits of the operand on the left of ">>" to the right by a number of bits specified by the operand on the right of ">>". | A >> 2 results in 15, binary 0000 1111 |
The following example demonstrates the usage of bitwise operators:
Example
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 - the value of c is %d\n", c )
c = a | b /* 61 = 0011 1101 */
fmt.Printf("Line 2 - the value of c is %d\n", c )
c = a ^ b /* 49 = 0011 0001 */
fmt.Printf("Line 3 - the value of c is %d\n", c )
c = a << 2 /* 240 = 1111 0000 */
fmt.Printf("Line 4 - the value of c is %d\n", c )
c = a >> 2 /* 15 = 0000 1111 */
fmt.Printf("Line 5 - the value of c is %d\n", c )
}
Running 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 language.
| Operators | Description | Example |
|---|---|---|
| = | Simple assignment operator, which assigns the value of an expression to an lvalue. | C = A + B assigns the result of the expression A + B to C |
| += | Add and assign | C += A is equal to C = C + A |
| -= | Subtract and assign | C -= A is equal to C = C - A |
| *= | Multiply and assign | C *= A is equal to C = C * A |
| /= | Divide and assign | C /= A is equal to C = C / A |
| %= | Modulo and assign | C %= A is equal to C = C % A |
| <<= | Left shift assignment | C <<= 2 is equivalent to C = C << 2 |
| >>= | Right shift assignment | 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 examples demonstrate the usage of the assignment operators:
Example
import "fmt"
func main() {
var a int = 21
var c int
c = a
fmt.Printf(Line 1 - = operator example, c value = %d\n", c )
c += a
fmt.Printf(Line 2 - += operator example, c value = %d\n", c )
c -= a
fmt.Printf(Line 3 - -= operator example, c value = %d\n", c )
c *= a
fmt.Printf(Line 4 - *= operator example, c value = %d\n", c )
c /= a
fmt.Printf(Line 5 - /= operator example, c value = %d\n", c )
c = 200;
c <<= 2
fmt.Printf(Line 6 - <<= operator example, c value = %d\n", c )
c >>= 2
fmt.Printf(Line 7 - >>= operator example, c value = %d\n", c )
c &= 2
fmt.Printf(Line 8 - &= operator example, c value = %d\n", c )
c ^= 2
fmt.Printf(Line 9 - ^= operator example, c value = %d\n", c )
c |= 2
fmt.Printf(Line 10 - |= operator example, c value = %d\n", c )
}
Running 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 language.
| Operators | Description | Example |
|---|---|---|
| & | Return the storage address of a variable | &a; will give the actual address of the variable. |
| * | Pointer variable. | *a; is a pointer variable |
The following examples demonstrate the usage of other operators:
Example
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 = %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' contains the address of variable 'a' */
fmt.Printf("The value of a is %d\n", a);
fmt.Printf("*ptr is %d\n", *ptr);
}
Running 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. The evaluation direction of binary operators is always from left to right. The following table lists all operators and their precedence, from top to bottom representing precedence from high to low:
| Precedence | Operators |
|---|---|
| 5 | * / % << >> & &^ |
| 4 | + - | ^ |
| 3 | == != < <= > >= |
| 2 | && |
| 1 | || |
Of course, you can use parentheses to temporarily raise the overall operation precedence of an expression.
Running result of the above example:
Example
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("The value of (a + b) * c / d is : %d\n", e );
e = ((a + b) * c) / d; // (30 * 15 ) / 5
fmt.Printf("The value of ((a + b) * c) / d is : %d\n" , e );
e = (a + b) * (c / d); // (30) * (15/5)
fmt.Printf("The value of (a + b) * (c / d) is : %d\n", e );
e = a + (b * c) / d; // 20 + (150/5)
fmt.Printf("The value of a + (b * c) / d is : %d\n" , e );
}
Running result of the above example:
(a + b) * c / d 的值为 : 90 ((a + b) * c) / d 的值为 : 90 (a + b) * (c / d) 的值为 : 90 a + (b * c) / d 的值为 : 50other extensions