Go Language Operators
Operators are used to perform mathematical or logical operations during program execution.
Go language includes the following built-in operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Other Operators
Let's take a detailed look at each operator.
Arithmetic Operators
The table below lists all the arithmetic operators in Go language. Assume A is 10 and B is 20.
Operator | Description | Example |
---|---|---|
+ | Addition | A + B outputs 30 |
- | Subtraction | A - B outputs -10 |
* | Multiplication | A * B outputs 200 |
/ | Division | B / A outputs 2 |
% | Modulus | B % A outputs 0 |
++ | Increment | A++ outputs 11 |
-- | Decrement | A-- outputs 9 |
Here is an example demonstrating the usage of each arithmetic operator:
Example
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 = 21 // For testing purposes, a is reassigned to 21
a--
fmt.Printf("Line 7 - Value of a is %d\n", a )
}
Output of the above example:
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 20
Relational Operators
The table below lists all the relational operators in Go language. Assume A is 10 and B is 20.
Operator | Description | Example |
---|---|---|
== | Checks if the values of two operands are equal, if yes then condition becomes true. | (A == B) is False |
!= | Checks if the values of two operands are not equal, if values are not equal then condition becomes true. | (A != B) is True |
> | Checks if the value of the left operand is greater than the value of the right operand, if yes then condition becomes true. | (A > B) is False |
< | Checks if the value of the left operand is less than the value of the right operand, if yes then condition becomes true. | (A < B) is True |
>= | Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes then condition becomes true. | (A >= B) is False |
<= | Checks if the value of the left operand is less than or equal to the value of the right operand, if yes then condition becomes true. | (A <= B) is True |
Here is an example demonstrating the usage of relational operators:
Example
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" )
}
/* Let's change the values 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 a\n" )
}
}
The above example execution results:
First line - a is not equal to b
Second line - a is not less than b
Third line - a is greater than b
Fourth line - a is less than or equal to b
Fifth line - b is greater than or equal to a
Logical Operators
The following table lists all the logical operators in Go language. Assume A is True and B is False.
Operator | Description | Example |
---|---|---|
&& | Logical AND operator. If both operands are True, the condition is True, otherwise False. | (A && B) is False |
|| | Logical OR operator. If any of the two operands is True, the condition is True, otherwise False. | (A || B) is True |
! | Logical NOT operator. If the condition is True, the logical NOT condition is False, otherwise 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 line - 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 above example execution results:
Second line - Condition is true
Third line - Condition is false
Fourth line - Condition is true
Bitwise Operators
Bitwise operators work on bits and perform bit-by-bit operations.
The following table shows the calculations for 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 |
Assume A = 60; B = 13; their binary conversions are:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
Go language supports the following bitwise operators. Assume A is 60 and B is 13:
Operator | Description | Example |
---|---|---|
& | Bitwise AND operator. It performs a bitwise AND operation on the operands. | (A & B) results in 12, binary 0000 1100 |
| | Bitwise OR operator. It performs a bitwise OR operation on the operands. | (A | B) results in 61, binary 0011 1101 |
^ | Bitwise XOR operator. It performs a bitwise XOR operation on the operands. | (A ^ B) results in 49, binary 0011 0001 |
<< | Left shift operator. It shifts the bits of the left operand to the left by the number of positions specified by the right operand. | A << 2 results in 240, binary 1111 0000 |
>> | Right shift operator. It shifts the bits of the left operand to the right by the number of positions specified by the right operand. | A >> 2 results in 15, binary 0000 1111 |
The following example demonstrates the use of bitwise operators:
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("First line - the value of c is %d\n", c)
c = a | b /* 61 = 0011 1101 */
fmt.Printf("Second line - the value of c is %d\n", c)
c = a ^ b /* 49 = 0011 0001 */
fmt.Printf("Third line - the value of c is %d\n", c)
c = a << 2 /* 240 = 1111 0000 */
fmt.Printf("Fourth line - the value of c is %d\n", c)
c = a >> 2 /* 15 = 0000 1111 */
fmt.Printf("Fifth line - the value of c is %d\n", c)
}
The above example results:
First line - the value of c is 12
Second line - the value of c is 61
Third line - the value of c is 49
Fourth line - the value of c is 240
Fifth line - the value of c is 15
Assignment Operators
The table below lists all the assignment operators in Go language.
Operator | Description | Example | |||
---|---|---|---|---|---|
= | Simple assignment operator, assigns the value of an expression to a left operand | C = A + B assigns the result of A + B to C | |||
+= | Add AND assignment operator | C += A is equivalent to C = C + A | |||
-= | Subtract AND assignment operator | C -= A is equivalent to C = C - A | |||
*= | Multiply AND assignment operator | C *= A is equivalent to C = C * A | |||
/= | Divide AND assignment operator | C /= A is equivalent to C = C / A | |||
%= | Modulus AND assignment operator | C %= A is equivalent to C = C % A | |||
<<= | Left shift AND assignment operator | C <<= 2 is equivalent to C = C << 2 | |||
>>= | Right shift AND assignment operator | C >>= 2 is equivalent to C = C >> 2 | |||
&= | Bitwise AND assignment operator | C &= 2 is equivalent to C = C & 2 | |||
^= | Bitwise XOR assignment operator | C ^= 2 is equivalent to C = C ^ 2 | |||
= | Bitwise OR assignment operator | C | = 2 is equivalent to C = C | 2 |
The following example demonstrates the usage of assignment operators:
Example
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 )
}
The above example results:
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
Other Operators
The table below lists other operators in Go language.
| Operator | Description | Example | | --- | --- | --- | This is a Chinese to English translation. Here is the English translation of the text:
| & | Return variable storage address | &a; will give the actual address of the variable. | | * | Pointer variable | *a; is a pointer variable |
The following example demonstrates the usage of other operators:
Example
package main
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 );
/* & and * operator example */
ptr = &a /* 'ptr' contains the address of variable 'a' */
fmt.Printf("Value of a is %d\n", a);
fmt.Printf("*ptr is %d\n", *ptr);
}
The output of the above example:
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
Operator Precedence
Some operators have higher precedence, and binary operators are evaluated from left to right. The table below lists all operators and their precedence, from highest to lowest:
Precedence | Operator | ||
---|---|---|---|
5 | * / % << >> & &^ | ||
4 | + - | ^ | |
3 | == != < <= > >= | ||
2 | && | ||
1 |
Of course, you can use parentheses to temporarily increase the precedence of an entire expression.
The output of the above example:
Example
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("(a + b) * c / d is : %d\n", e );
e = ((a + b) * c) / d; // (30 * 15 ) / 5
fmt.Printf("((a + b) * c) / d is : %d\n" , e );
e = (a + b) * (c / d); // (30) * (15/5)
fmt.Printf("(a + b) * (c / d) is : %d\n", e );
e = a + (b * c) / d; // 20 + (150/5)
fmt.Printf("a + (b * c) / d is : %d\n" , e );
}
The output of the above example:
(a + b) * c / d is : 90
((a + b) * c) / d is : 90
(a + b) * (c / d) is : 90
a + (b * c) / d is : 50