Golang Programming Operators
Operators are symbols that tell the compiler to perform specific operations that may be mathematical, relational or logical. The Golang programming language has a rich in in-built operators that can be grouped into: Arithmetic Operators, Relational Operators, Logical Operators, Bitwise Operators, Assignment Operators and the Misc Operators.
So, let’s dive in and find out about these operators:
#1: Arithmetic Operators
Here are examples of Golang’s arithmetic operators and their uses:
-
- • “
+
-
- ” It adds two operands. For instance if A=20 and B=20 then A+ gives 40 as the answer.
-
- • “
–
-
- ” It subtracts the second operand from the first operand. For example, if A=20 and B=20 then A – B will yield 0.
-
- • “
*
-
- ” It’s used for multiplication of operands. For example, A*B produces 400.
-
- • “
/
-
- ” It’s used for division. For instance, A/B produces 1.
-
- • “
%
-
- ” It’s a modulus operator—it displays the remainder of a number after an integer division. For instance, B % A yields 0 as a remainder.
-
- • “
++
-
- ” It’s an increment operator—it increases integer value by one. For instance, A++ yields 21.
-
- • “
—
- ” It’s a decrements operator—it decreases an integer value by one.
#2: Relational Operators
Here are examples of Golang’s relational operators and their uses:
-
- • “
==
-
- ” It checks whether the values of two operands are equal or not. If they are equal, the condition becomes true. For instance A==B is true if A=20 and B=20.
-
- • “
!=
-
- ” It checks whether the values of two operands are equal or not. For instance, A!=B is false.
-
- • “
>
-
- ” It checks whether the value of the left operand is greater than what is on the right operand.
-
- • “
<
-
- ” It checks whether the value of the left operand is less than what is on the right operand.
-
- • “
>=
-
- ” It checks whether the value of the left operand is greater than or equal to what is on the right operand.
-
- • “
<=
- ” It checks whether the value of the left operand is less than or equal to what is on the right operand.
#3: Logical Operators
Here are examples of Golang’s logical operators and their uses:
-
- • “
&&
-
- ” the logical AND operator. If both the operands are not zero, then condition is true.
-
- • “
||
-
- ” the logical OR operator. If any of two operands is not zero, then condition is true.
-
- • “
!
- ” the logical NOT Operator. It reverses the logical state of its initial operand.
#4: Bitwise Operators
The bitwise operators performs bit-by-bit operations in programming. Here are examples of Golang’s bitwise operators and their uses:
-
- • “
&
-
- ” the binary AND operator. It copies a bit to the result if it is existing in both the operands.
-
- • “
|
-
- ” the binary OR operator. It copies a bit if it is existing in either operand.
-
- • “
^
-
- ” the binary XOR operator. It copies the bit if it is set in only one operand and not both operators.
-
- • “
<<
-
- ” the binary left shift operator. It moves the left side by a number of bits that are specified by the right operand.
-
- • “
>>
- ” the binary right shift operator. It moves the right side by a number of bits that are specified by the right operand.
#5: Assignment Operators
Here are examples of Golang’s assignment operators and their uses:
-
- • “
=
-
- ” It assigns values from right side operands to the left side operand.
-
- • “
+=
-
- ” It adds the right operand to left operand and assign the answer to left operand.
-
- • “
-=
-
- ” It subtracts the right operand to left operand and assign the answer to left operand.
-
- • “
*=
-
- ” It multiplies the right operand to left operand and assign the answer to left operand.
-
- • “
/=
- ” It divides the right operand to left operand and assign the answer to left operand.
#6: Misc Operators
Here are examples of Golang’s misc operators and their uses:
-
- • “
&
-
- ” It returns the address of a given variable.
-
- • “
*
- ” It’s a pointer to a particular variable.
Now you can begin using Golang’s operators.
GET YOUR FREE Golang EBOOK!