Operators in Golang
Learn Data Science, Machine Learning and Artificial Intelligence for Free
Join Codersdaily
SUBSCRIBEIn this tutorial we'll learn about operators in Golang. Operators are used to perform various operations on data such as arithmetic, assignment, comparison, logical, and more. Understanding these operators is essential for writing effective and efficient code.
1. Arithmetic Operators:
Arithmetic operators are used to perform arithmetic operations like addition, subtraction, multiplication, division etc.
Here, is the list of some commonly used Arithmetic operators in Golang.

Addition (+): It is used two operands together.

Subtraction (): It is used to subtract the second operand from the first.

Multiplication (*): It is used to multiply two operands.

Division (/): It is used to divide the first operand by the second.

Modulus (%): It returns the remainder after division.
2. Assignment Operators:
Arithmetic operators are used to assign values to variables.
Here, is the list of some commonly used assignment operators in Golang.

Assignment (=): It Assigns the value on the right to the variable on the left.

Addition assignment (+=): It is used to add the right operand to the left operand and assigns the result to the left operand.

Subtraction assignment (=): It is used to subtract the right operand from the left operand and assigns the result to the left operand.

Multiplication assignment (*=): It is used to multiply the right operand with the left operand and assigns the result to the left operand.

Division assignment (/=): It is used to divide the left operand by the right operand and assigns the result to the left operand.

Modulus assignment (%=): It calculates the modulus of the left operand with the right operand and assigns the result to the left operand.
3. Relational Operators:
Relational operators are used to compare two values and return a boolean result based on the comparison.
Here, is the list of some commonly used comparison operators in Golang.

Equal to (==): It checks if the operands are equal.

Not equal to (!=): It checks if the operands are not equal.

Greater than (>): It checks if the left operand is greater than the right operand.

Less than (<): It checks if the left operand is less than the right operand.

Greater than or equal to (>=): It checks if the left operand is greater than or equal to the right operand.

Less than or equal to (<=): It checks if the left operand is less than or equal to the right operand.
4. Logical Operators:
Logical operators are used to perform logical operations on boolean values.
Here, is the list of some commonly used logical operators in Golang.

Logical AND (&&): Returns true if both operands are true.

Logical OR (): Returns true if either of the operands is true.

Logical NOT (!): Inverts the logical state of the operand.
5. Bitwise Operators:
Bitwise operators are used to perform bitwise operations on boolean values.
Here, is the list of some commonly used Bitwise operators in Golang.

Bitwise AND (&): Performs a bitwise AND operation on the operands.

Bitwise OR (): Performs a bitwise OR operation on the operands.

Bitwise XOR (^): Performs a bitwise XOR (exclusive OR) operation on the operands.

Bitwise complement (~): Inverts the bits of the operand.

Left shift (<<): Shifts the bits of the left operand to the left by the number of positions specified by the right operand.

Right shift (>>): Shifts the bits of the left operand to the right by the number of positions specified by the right operand.
6. Other Operators:

Address (&): Returns the address of a variable.

Dereference (*): Accesses the value stored at a particular memory address.
Example:
package main
import "fmt"
func main() {
var num1, num2 int = 10, 5
// Arithmetic Operator: Multiplication
product := num1 * num2
fmt.Println("Product:", product)
// Assignment Operator: Compound Addition
num1 += num2
fmt.Println("Compound Addition:", num1)
// Relational Operator: Greater Than
isGreater := num1 > num2
fmt.Println("Is num1 greater than num2?", isGreater)
// Logical Operator: Logical AND
isBothTrue := isGreater && (num1 == 15)
fmt.Println("Is num1 greater than num2 and equal to 15?", isBothTrue)
// Bitwise Operator: Bitwise OR
bitwiseOR := num1  num2
fmt.Println("Bitwise OR:", bitwiseOR)
}
Output:
Product: 50
Compound Addition: 15
Is num1 greater than num2? true
Is num1 greater than num2 and equal to 15? true
Bitwise OR: 15