Go language operator
Operators are used to perform mathematical or logical operations while the program is running.
Go
operators built into the language are:
Arithmetic operator
Relational operator
Logical operator
Bit operator
Assignment operator
Other operators
Next, let’s take a closer look at the introduction of each operator.
Arithmetic operator
The following table lists the arithmetic operators for all Go
languages.Assuming the value of A
is 10
and the value of B
is 20
.
Operator |
Description |
Example |
---|---|---|
|
Add up |
A + B output result 30 |
|
Subtract |
A-B output result-10 |
|
Multiply |
A * B output result 200 |
|
Divide |
B / A output result 2 |
|
Beg for surplus |
B % A output result 0 |
|
Self-increasing |
A++ output result 11 |
|
Self-subtraction |
A– output result 9 |
The following example demonstrates the use of individual arithmetic operators:
Example
package main
import "fmt"
func main() {
var a int = 21
var b int = 10
var c int
c = a + b
fmt.Printf("The value of the first line - c is %d\\n", c )
c = a - b
fmt.Printf("The value of the second line - c is %d\\n", c )
c = a * b
fmt.Printf("The value of the third line- c is %d\\n", c )
c = a / b
fmt.Printf("The value of the fourth line- c %d\\n", c )
c = a % b
fmt.Printf("The value of the fifth line- c %d\\n", c )
a++
fmt.Printf("The value of the sixth line- a %d\\n", a )
a=21 // For the convenience of testing, a is reassigned to 21 here
a--
fmt.Printf("The value of line 7- a is %d\\n", a )
}
The running result of the above instance:
The value of line 1- c is 31
The value of line 2- c is 11
The value of line 3- c is 210
The value of line 4- c is 2
The value of line 5- c is 1
The value of line 6- a is 22
The value of line 7- a is 20
Relational operator
The following table lists the relational operators for all Go
languages.Assuming the value of A
is 10
and the value of B
is 20
.
Operator |
Description |
Example |
---|---|---|
|
Check whether the two values are equal, and return True if they are equal, otherwise return False. |
(A == B) is False |
|
Check whether the two values are not equal, and return True if they are not equal, otherwise return False. |
(A! = B) is True |
|
Check whether the value on the left is greater than the value on the right, and return False if it returns True. |
(A > B) is False |
|
Check whether the value on the left is less than the value on the right, andreturn False if it returns True. |
(A < B) is True |
|
Check whether the value on the left is greater than or equal to the value on the right, and return False if it returns True. |
(A >= B) is False |
|
Check whether the value on the left is less than or equal to the value on the right, and return False if it returns True. |
(A <= B) is True |
The following example demonstrates the use of relational operators:
Example
package main
import "fmt"
func main() {
var a int = 21
var b int = 10
if( a == b ) {
fmt.Printf("First row - a equals b\\n" )
} else {
fmt.Printf("The first line - a is not equal to b\\n" )
}
if ( a < b ) {
fmt.Printf("Second row - a is less than b\\n" )
} else {
fmt.Printf("Second row - a not less than b\\n" )
}
if ( a > b ) {
fmt.Printf("Third row - a greater than b\\n" )
} else {
fmt.Printf("Third row - a not greater than b\\n" )
}
/* Lets change value of a and b */
a = 5
b = 20
if ( a <= b ) {
fmt.Printf("Fourth line - a is less than or equal to b\\n" )
}
if ( b >= a ) {
fmt.Printf("The fifth row - b is greater than or equal to a\\n" )
}
}
The running result of the above instance:
The first line - a is not equal to b
Second row - a not less than b
Third row - a greater than b
Fourth line - a is less than or equal to b
The fifth line - b is greater than or equal to a
Logical operator
The following table lists the logical operators for all Go
languages. Assuming that the value of A
is True
and the value of B
is False
.
Operator |
Description |
Example |
---|---|---|
&& |
Logical AND operator. Conditional True if both operands are True, otherwise False. |
(A && B) is False |
|| |
Logical OR operator. If there is one Operand on both sides True, then conditional True, otherwise False. |
(A || B) is True |
! |
Logical NOT operator. If the condition is True, the logical NOT condition False, otherwise it is True. |
!(A && B) is True |
The following example demonstrates the use of logical operators:
Example
package main
import "fmt"
func main() {
var a bool = true
var b bool = false
if ( a && b ) {
fmt.Printf("First row - condition is true\\n" )
}
if ( a \|\| b ) {
fmt.Printf("Second line - condition is true\\n" )
}
/* Modify the values of a and b */
a = false
b = true
if ( a && b ) {
fmt.Printf("Third line - condition is true\\n" )
} else {
fmt.Printf("Third line - condition is false\\n" )
}
if ( !(a && b) ) {
fmt.Printf("Fourth line - condition is true\\n" )
}
}
The running result of the above instance:
Second line - condition is true
Third line - condition is false
Fourth line - condition is true
Bit operator
The bit operator operates on the binary bits of integers in memory.
The following table lists the calculations of bit 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 |
Assuming A = 60
; B = 13
; Its binary number is converted to:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
The bit operators supported by the Go
language are shown ithe table below. Assuming that A
is 60
and B
is 13
:
Operator |
Description |
Example |
---|---|---|
|
The bitwise and operator “&” are binocular operators. Its function is the binary phase corresponding to each of the two numbers involved in the operation. |
(A & B) the result is 12 and the binary is 0000 1100 |
|
The bitwise or operator “|” is a binocular operator. Its function is the binary phase corresponding to each of the two numbers involved in the operation. |
(A | B) the result is 61 and the binary is 0011 1101 |
|
The bitwise XOR operator “^” is a binocular operator. Its function is that the binary bits corresponding to each of the two numbers involved in theoperation are different. When the corresponding binary bits are different, the result is 1. |
(A ^ B) the result is 49 and the binary is 0011 0001 |
|
The left shift operator “<<” is a binocular operator. Moving n bits to theleft is multiplied by 2 to the n power. Its function shifts all the binary bits of the operands on the left side of “<<” to the left, and the number on the right side of “<<” specifies the number of bits to be moved, discarded in high order and 0 in low position. |
A << 2, the result is 240. the binary is 1111 0000. |
|
The right shift operator “>>” is a binocular operator. Moving n bits to the right is divided by 2 to the n power. Its function is to move all the binaries of the operands on the left to the right, and the number on the right specifies the number of digits to move. |
A >> 2 result is 15, binary is 0000 1111 |
The following example demonstrates the use of the bit operator:
Example
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("The value of line 1- c is%d\\n", c )
c = a \| b /* 61 = 0011 1101 */
fmt.Printf("The value of line 2- c is%d\\n", c )
c = a ^ b /* 49 = 0011 0001 */
fmt.Printf("The value of line 3- c is%d\\n", c )
c = a << 2 /* 240 = 1111 0000 */
fmt.Printf("The value of line 4- c is%d\\n", c )
c = a >> 2 /* 15 = 0000 1111 */
fmt.Printf("The value of line 5- c is%d\\n", c )
}