Go Operators
Operators are used to perform mathematical or logical operations during program execution.
Go language has 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 following table lists all arithmetic operators in Go. Assume A = 10, B = 20.
| Operator | Description | Example |
| --- | --- | --- |
| + | Adds two operands | A + B outputs 30 |
| - | Subtracts the second operand from the first | A - B outputs -10 |
| * | Multiplies both operands | A * B outputs 200 |
| / | Divides the numerator by the denominator | B / A outputs 2 |
| % | Modulus operator. Gives the remainder after integer division | B % A outputs 0 |
| ++ | Increment operator. Increases the integer value by one | A++ outputs 11 |
| -- | Decrement operator. Decreases the integer value by one | A-- outputs 9 |
The following example demonstrates the use of 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("First line - Value of c is %dn", c )
c = a - b
fmt.Printf("Second line - Value of c is %dn", c )
c = a * b
fmt.Printf("Third line - Value of c is %dn", c )
c = a / b
fmt.Printf("Fourth line - Value of c is %dn", c )
c = a % b
fmt.Printf("Fifth line - Value of c is %dn", c )
a++
fmt.Printf("Sixth line - Value of a is %dn", a )
a=21// Reassign a to 21 for testing convenience
a--
fmt.Printf("Seventh line - Value of a is %dn", a )
}
The output of the above example:
First line - Value of c is 31Second line - Value of c is 11Third line - Value of c is 210Fourth line - Value of c is 2Fifth line - Value of c is 1Sixth line - Value of a is 22Seventh line - Value of a is 20
* * *
## Relational Operators
The following table lists all relational operators in Go. Assume A = 10, B = 20.
| Operator | Description | Example |
| --- | --- | --- |
| == | Checks if the values of two operands are equal or not. If yes, the condition is true. | (A == B) is False |
| != | Checks if the values of two operands are equal or not. If values are not equal, the condition is true. | (A != B) is True |
| > | Checks if the value of the left operand is greater than the right operand. If yes, the condition is true. | (A > B) is False |
| < | Checks if the value of the left operand is less than the right operand. If yes, the condition is true. | (A = | Checks if the value of the left operand is greater than or equal to the right operand. If yes, the condition is true. | (A >= B) is False |
| <= | Checks if the value of the left operand is less than or equal to the right operand. If yes, the condition is 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 line - a is equal to bn")
}else{
fmt.Printf("First line - a is not equal to bn")
}
if( a b ){
fmt.Printf("Third line - a is greater than bn")
}else{
fmt.Printf("Third line - a is not greater than bn")
}
/* Lets change value of a and b */
a =5
b =20
if( a = a ){
fmt.Printf("Fifth line - b is greater than or equal to an")
}
}
The output of the above example:
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 logical operators in Go. Assume A = True, B = False.
| Operator | Description | Example |
| --- | --- | --- |
| && | Logical AND operator. If both operands are true, then the condition becomes true. | (A && B) is False |
| || | Logical OR operator. If any of the two operands is true, then the condition becomes true. | (A || B) is True |
| ! | Logical NOT operator. Used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. | !(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 truen")
}
if( a || b ){
fmt.Printf("Second line - Condition is truen")
}
/* Change value of a and b */
a =false
b =true
if( a && b ){
fmt.Printf("Third line - Condition is truen")
}else{
fmt.Printf("Third line - Condition is falsen")
}
if(!(a && b)){
fmt.Printf("Fourth line - Condition is truen")
}
}
The output of the above example:
Second line - Condition is trueThird line - Condition is falseFourth line - Condition is true
* * *
## Bitwise Operators
Bitwise operators work on bits and perform bit-by-bit operation.
The following table lists 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; In binary format, they will be:
A = 0011 1100 B = 0000 1101----------------- A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0001
Go supports bitwise operators as shown in the following table. Assume A = 60, B = 13:
| Operator | Description | Example |
| --- | --- | --- |
| & | Binary AND Operator copies a bit to the result if it exists in both operands. | (A & B) outputs 12, which is 0000 1100 in binary |
| | | Binary OR Operator copies a bit if it exists in either operand. | (A | B) outputs 61, which is 0011 1101 in binary |
| ^ | Binary XOR Operator copies the bit if it is set in one operand but not both. | (A ^ B) outputs 49, which is 0011 0001 in binary |
| << | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. | A <> | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. | A >> 2 outputs 15, which is 0000 1111 in binary |
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 - Value of c is %dn", c )
c = a | b /* 61 = 0011 1101 */
fmt.Printf("Second line - Value of c is %dn", c )
c = a ^ b /* 49 = 0011 0001 */
fmt.Printf("Third line - Value of c is %dn", c )
c = a <>2/* 15 = 0000 1111 */
fmt.Printf("Fifth line - Value of c is %dn", c )
}
The output of the above example:
First line - Value of c is 12Second line - Value of c is 61Third line - Value of c is 49Fourth line - Value of c is 240Fifth line - Value of c is 15
* * *
## Assignment Operators
The following table lists all assignment operators in Go.
| Operator | Description | Example |
| --- | --- | --- |
| = | Simple assignment operator. Assigns values from the right side operand to the left side operand | C = A + B assigns the value of A + B into C |
| += | Add AND assignment operator. It adds the right operand to the left operand and assigns the result to the left operand. | C += A is equivalent to C = C + A |
| -= | Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. | C -= A is equivalent to C = C - A |
| *= | Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand. | C *= A is equivalent to C = C * A |
| /= | Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand. | C /= A is equivalent to C = C / A |
| %= | Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. | C %= A is equivalent to C = C % A |
| <<= | Left shift AND assignment operator. | C <<= 2 is same as C = C <>= | Right shift AND assignment operator. | C >>= 2 is same as C = C >> 2 |
| &= | Bitwise AND assignment operator. | C &= 2 is same as C = C & 2 |
| ^= | Bitwise exclusive OR and assignment operator. | C ^= 2 is same as C = C ^ 2 |
| |= | Bitwise inclusive OR and assignment operator. | C |= 2 is same as C = C | 2 |
The following example demonstrates the use 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 = %dn", c )
c +=a
fmt.Printf("Line 2 - += Operator Example, value of c = %dn", c )
c -=a
fmt.Printf("Line 3 - -= Operator Example, value of c = %dn", c )
c *=a
fmt.Printf("Line 4 - *= Operator Example, value of c = %dn", c )
c /=a
fmt.Printf("Line 5 - /= Operator Example, value of c = %dn", c )
c =200;
c <<=2
fmt.Printf("Line 6 - <>=2
fmt.Printf("Line 7 - >>= Operator Example, value of c = %dn", c )
c &=2
fmt.Printf("Line 8 - &= Operator Example, value of c = %dn", c )
c ^=2
fmt.Printf("Line 9 - ^= Operator Example, value of c = %dn", c )
c |=2
fmt.Printf("Line 10 - |= Operator Example, value of c = %dn", c )
}
The output of the above example:
Line 1 - = Operator Example, value of c = 21Line 2 - += Operator Example, value of c = 42Line 3 - -= Operator Example, value of c = 21Line 4 - *= Operator Example, value of c = 441Line 5 - /= Operator Example, value of c = 21Line 6 - <>= Operator Example, value of c = 200Line 8 - &= Operator Example, value of c = 0Line 9 - ^= Operator Example, value of c = 2Line 10 - |= Operator Example, value of c = 2
* * *
## Other Operators
The following table lists some other operators supported by Go.
| Operator | Description | Example |
| --- | --- | --- |
| & | Returns the address of a variable. | &a; gives the actual address of the variable. |
| * | Pointer to a variable. | *a; is a pointer to a variable |
The following example demonstrates the use of other operators:
## Example
package main
import"fmt"
func main(){
var a int=4
var b int32
var c float32
var ptr *int
/* Operator examples */
fmt.Printf("Line 1 - Type of variable a = %Tn", a );
fmt.Printf("Line 2 - Type of variable b = %Tn", b );
fmt.Printf("Line 3 - Type of variable c = %Tn", c );
/* & and * operator examples */
ptr =&a /* 'ptr' now contains the address of 'a' */
fmt.Printf("Value of a is %dn", a);
fmt.Printf("*ptr is %dn",*ptr);
}
The output of the above example:
Line 1 - Type of variable a = intLine 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 than others. For example, the multiplication operator has higher precedence than the addition operator.
The following table lists the operators in order of precedence from highest to lowest:
| Precedence | Operator |
| --- | --- |
| 5 | * / % <>&&^ |
| 4 | + - | ^ |
| 3 | == != <>= |
| 2 | && |
| 1 | || |
Operators with higher precedence are evaluated first. For example, the expression `a + b * c` is evaluated as `a + (b * c)`.
You can use parentheses to override the precedence rules. For example, `(a + b) * c` is evaluated as `(a + b) * c`.
The following example demonstrates the use of operator precedence:
## 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("Value of (a + b) * c / d is : %dn",e );
e =((a + b)* c)/ d;// (30 * 15 ) / 5
fmt.Printf("Value of ((a + b) * c) / d is : %dn",e );
e =(a + b)*(c / d);// (30) * (15/5)
fmt.Printf("Value of (a + b) * (c / d) is : %dn",e );
e = a +(b * c)/ d;// 20 + (150/5)
fmt.Printf("Value of a + (b * c) / d is : %dn",e );
}
The output of the above example:
Value of (a + b) * c / d is : 90Value of ((a + b) * c) / d is : 90Value of (a + b) * (c / d) is : 90 Value of a + (b * c) / d is : 50
YouTip