Operators are the foundation of any programming language. Thus the functionality of the Go language is incomplete without the use of operators. Operators allow us to perform different kinds of operations on operands. In Go language, operators Can be categorized based upon their different functionality:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Misc Operators
Arithmetic Operators
These are used to perform arithmetic/mathematical operations on operands in Go language:
- Addition: The ‘+’ operator adds two operands. For example, x+y.
- Subtraction: The ‘-‘ operator subtracts two operands. For example, x-y.
- Multiplication: The ‘*’ operator multiplies two operands. For example, x*y.
- Division: The ‘/’ operator divides the first operand by the second. For example, x/y.
- Modulus: The ‘%’ operator returns the remainder when first operand is divided by the second. For example, x%y.
Example :-
// Go program to illustrate the // use of arithmatic operators package main import "fmt" func main() { p:= 34 q:= 20 // Addition result1:= p + q fmt.Printf("Result of p + q = %d", result1) // Subtraction result2:= p - q fmt.Printf("\nResult of p - q = %d", result2) // Multiplication result3:= p * q fmt.Printf("\nResult of p * q = %d", result3) // Division result4:= p / q fmt.Printf("\nResult of p / q = %d", result4) // Modulus result5:= p % q fmt.Printf("\nResult of p %% q = %d", result5) } |
Output:
Result of p + q = 54 Result of p - q = 14 Result of p * q = 680 Result of p / q = 1 Result of p % q = 14
Relational Operators
The following table lists all the relational operators supported by Go language. Assume variable A holds 10 and variable B holds 20, then −
Operator | Description | Example |
---|---|---|
== | It checks if the values of two operands are equal or not; if yes, the condition becomes true. | (A == B) is not true. |
!= | It checks if the values of two operands are equal or not; if the values are not equal, then the condition becomes true. | (A != B) is true. |
> | It checks if the value of left operand is greater than the value of right operand; if yes, the condition becomes true. | (A > B) is not true. |
< | It checks if the value of left operand is less than the value of the right operand; if yes, the condition becomes true. | (A < B) is true. |
>= | It checks if the value of the left operand is greater than or equal to the value of the right operand; if yes, the condition becomes true. | (A >= B) is not true. |
<= | It checks if the value of left operand is less than or equal to the value of right operand; if yes, the condition becomes true. | (A <= B) is true. |
Logical Operators
The following table lists all the logical operators supported by Go language. Assume variable A holds 1 and variable B holds 0, then −
Operator | Description | Example |
---|---|---|
&& | Called Logical AND operator. If both the operands are non-zero, then condition becomes true. | (A && B) is false. |
|| | Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true. | (A || B) is true. |
! | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(A && B) is true. |
The following table shows all the logical operators supported by Go language. Assume variable A holds true and variable B holds false, then −
Operator | Description | Example |
---|---|---|
&& | Called Logical AND operator. If both the operands are false, then the condition becomes false. | (A && B) is false. |
|| | Called Logical OR Operator. If any of the two operands is true, then the condition becomes true. | (A || B) is true. |
! | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. | !(A && B) is true. |
Bitwise Operators
In Go language, there are 6 bitwise operators which work at bit level or used to perform bit by bit operations. Following are the bitwise operators :
- & (bitwise AND): Takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
- | (bitwise OR): Takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1.
- ^ (bitwise XOR): Takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.
- << (left shift): Takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.
- >> (right shift): Takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.
- &^ (AND NOT): This is a bit clear operator.
Example:
// Go program to illustrate the // use of bitwise operators package main import "fmt" func main() { p:= 34 q:= 20 // & (bitwise AND) result1:= p & q fmt.Printf("Result of p & q = %d", result1) // | (bitwise OR) result2:= p | q fmt.Printf("\nResult of p | q = %d", result2) // ^ (bitwise XOR) result3:= p ^ q fmt.Printf("\nResult of p ^ q = %d", result3) // << (left shift) result4:= p << 1 fmt.Printf("\nResult of p << 1 = %d", result4) // >> (right shift) result5:= p >> 1 fmt.Printf("\nResult of p >> 1 = %d", result5) // &^ (AND NOT) result6:= p &^ q fmt.Printf("\nResult of p &^ q = %d", result6) } |
Output:
Result of p & q = 0 Result of p | q = 54 Result of p ^ q = 54 Result of p <> 1 = 17 Result of p &^ q = 34
Assignment Operators
Assignment operators are used to assigning a value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of the variable on the left side otherwise the compiler will raise an error. Different types of assignment operators are shown below:
- “=”(Simple Assignment): This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left.
- “+=”(Add Assignment): This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.
- “-=”(Subtract Assignment): This operator is combination of ‘-‘ and ‘=’ operators. This operator first subtracts the current value of the variable on left from the value on the right and then assigns the result to the variable on the left.
- “*=”(Multiply Assignment): This operator is combination of ‘*’ and ‘=’ operators. This operator first multiplies the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.
- “/=”(Division Assignment): This operator is combination of ‘/’ and ‘=’ operators. This operator first divides the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
- “%=”(Modulus Assignment): This operator is combination of ‘%’ and ‘=’ operators. This operator first modulo the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
- “&=”(Bitwise AND Assignment): This operator is combination of ‘&’ and ‘=’ operators. This operator first “Bitwise AND” the current value of the variable on the left by the value on the right and then assigns the result to the variable on the left.
- “^=”(Bitwise Exclusive OR): This operator is combination of ‘^’ and ‘=’ operators. This operator first “Bitwise Exclusive OR” the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
- “|=”(Bitwise Inclusive OR):This operator is combination of ‘|’ and ‘=’ operators. This operator first “Bitwise Inclusive OR” the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.
Example:
// Go program to illustrate the // use of assignment operators package main import "fmt" func main() { var p int = 45 var q int = 50 // “=”(Simple Assignment) p = q fmt.Println(p) // “+=”(Add Assignment) p += q fmt.Println(p) //“-=”(Subtract Assignment) p-=q fmt.Println(p) // “*=”(Multiply Assignment) p*= q fmt.Println(p) // “/=”(Division Assignment) p /= q fmt.Println(p) // “%=”(Modulus Assignment) p %= q fmt.Println(p) } |
Output:
50 100 50 2500 50 0
Misc Operators
- &: This operator returns the address of the variable.
- *: This operator provides pointer to a variable.
- <-:The name of this operator is receive. It is used to receive a value from the channel.
Example:
// Go program to illustrate the // use of Misc Operators package main import "fmt" func main() { a := 4 // Using address of operator(&) and // pointer indirection(*) operator b := &a fmt.Println(*b) *b = 7 fmt.Println(a) } |
Output:
4 7