Introduction to Go, Operators,
Any programming language comes with certain operators that are used to perform some logical/mathematical operations. Operators are typically constructed that typically work like functions. Operators are basically denoted by special characters and used for additions(+), subtractions(-) and even like assignments(=)
Languages generally have built-in operators, and some languages allow user-defined operators.
The elements or numbers on which the operator is applied are called operands. The position of an operator with respect to operands can be prefix, postfix or Infix.
Different Go Operators
Different operators are as follows:
- Arithmetic Operators
- Logical Operators
- Relational operators
- Bitwise Operators
- Assignment Operators
- Miscellaneous/Other operators
1. Arithmetic
Let us assume a=6, b=3
- Add: This is used for the addition of numbers
Eg: c = a+b ; //c gives 9
- Subtract: This is used for the subtraction of numbers
Eg: c = a-b; //c gives 3
- Multiply: This is used for the multiplication of numbers
Eg: c = a*b;//c gives 18
- Divide: This is used for the division of numbers
Eg: c = a/b;//c gives 2
- Modulus: This is used to get the remainder after a division of numbers
Eg: c = a%b; // c gives 0
- Increment: This is used for increasing the integer value by 1
Eg: a++ //a is now 7
- Decrement: This is used for decreasing the integer value by 1
Eg: a– //a is now 5
2. Logical
Logical operations like AND, OR operations, NOT are done by these operators.
Let x = 0, y=1
- AND (&&): If both operands are non-zero valued, the AND of them becomes true.
Eg: x && y = false
- OR (||): If either of the operands is non-zero, the OR of them becomes true
Eg: x||y = true
- NOT (!): Not is used to reverse the logical value of the operand.
Eg: !x = true
!y = false
!(x&&y) = true
3. Relational
Relational operators return true or false based on operations on operands.
Let a = 3; b = 4; c = 3;
- Equals (==): This returns true if the operands are equal
Eg: a==c returns true
a==b returns false
- NotEquals (!=): This returns true if operands are not equal
Eg: a!=c returns false
a!=b returns true
- GreaterThan (>): This returns true if the left operand is greater than the right
E.g.: a>b returns false
- LessThan (<): This returns true if the left operand is less than the right
E.g.: a<b returns true
- GreaterThanOrEqualTo (>=): This returns true if the left operand is greater than or equal to the right operand
Eg: a>=c returns true
- LessThanOrEqualTo (<=): This returns true if the left operand is greater than or equal to the right operand
E.g.: a<=b returns true
4. BitWise
These Operators work bit by bit and output the logic based on bit operations.
Below is the truth table for reference:
X |
Y | X&Y | X|Y | X^Y |
0 |
0 | 0 | 0 |
0 |
0 |
1 | 0 | 1 |
1 |
1 |
0 | 0 | 1 |
1 |
1 | 1 | 1 | 1 |
0 |
Eg: Let A = 10, B = 13
A = 0000 1100
B = 0000 1101
- &: Used to perform bitwise AND Operation
Eg: A & B = 0000 1100 = 12
- |: Used to perform bitwise OR Operation
Eg: A | B = 0000 1101 = 13
- ^: Used to perform XOR operation
Eg: A ^ B = 0000 0001 = 1
- <<: This is the left shift which is used to shift the bits to the left by a number that is specified on the right
E.g., A << 2: This would shift the A value by 2
i.e: 0000 0010
0000 1000 = 8
- >>: This is the right shift which is used to shift the bits to the right by a number that is specified on the right
E.g., A >> 1: This would shift the A value by 1
i.e: 0000 0010
0000 0001 = 1
5. Assignment
These are used for assigning values from the right operand to the left based on the operator.
Let a=4, b = 2
- =: Used to assign the value simply from right to the left.
Eg: a = b //Assigns b value i.e 2 to a
- +=: Used to add the value and assign it to the left operand
Eg: a+=b => a=a+b => a = 6
- -=: Used to subtract the value and assign it to the left operand
Eg: a-=b => a=a-b => a = 2
- *=: Used to multiply the value and assign it to the left operand
Eg: a*=b => a=a*b => a = 8
- /=: Used to divide the value and assign it to the left operand
Eg: a/=b => a=a/b => a = 2
- %=: Used to assign the reminder after division
Eg: a%=b => a=a%b => a=0
- <<=: Used to left shift and assign the value to the variable
Eg: a<<=2 => =a<<2
- >>=: Used to right shift and assign the value to the variable
Eg: b>>=2 => =b>>2
- &=: Bitwise AND Assignment.
Eg: c&=2 => c = c&2
- |=: Bitwise OR Assignment.
Eg: c|=2 => c = c|2
- ^=: Bitwise XOR Assignment.
Eg: c^=2 => c = c^2
6. Other/Miscellaneous
& and * are two more operators that Go supports
- &: This is used to get the actual address of a variable
Usage: &a;
- *: This is used to get the pointer to a variable
Usage: *a
Recommended Articles
This has been a guide to Go Operators. Here we have discussed different types of Go Operators with examples for better understanding. You may also look at the following article to learn more –
4 Online Courses | 2 Hands-on Project | 17+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses