Go Operators
Operators in Go are special symbols used to perform operations on variables and values. They form the foundation of expressions and are essential for writing efficient and logical programs.
What Are Operators in Go?
An operator performs a specific task such as addition, comparison, or logical evaluation.
For example, the + operator is used to add values:
Example: Basic Addition
Using Operators with Variables
Operators can also work with variables and expressions, not just fixed values.
Example: Operations with Variables
Types of Operators in Go
Go provides several categories of operators, each designed for a specific purpose.
1. Arithmetic Operators
These operators perform basic mathematical calculations.
| Operator | Description | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus (remainder) | a % b |
Example
2. Assignment Operators
Assignment operators are used to assign values to variables.
| Operator | Example | Equivalent To |
|---|---|---|
| = | x = 10 | Assign value |
| += | x += 5 | x = x + 5 |
| -= | x -= 3 | x = x - 3 |
| *= | x *= 2 | x = x * 2 |
| /= | x /= 4 | x = x / 4 |
Example
3. Comparison Operators
These operators compare two values and return a boolean result (true or false).
| Operator | Description | Example |
|---|---|---|
| == | Equal to | a == b |
| != | Not equal to | a != b |
| > | Greater than | a > b |
| < | Less than | a < b |
| >= | Greater or equal | a >= b |
| <= | Less or equal | a <= b |
Example
4. Logical Operators
Logical operators are used to combine multiple conditions.
| Operator | Description | Example |
|---|---|---|
| && | Logical AND | a > 5 && b < 10 |
| || | Logical OR | a > 5 || b < 10 |
| ! | Logical NOT | !(a > b) |
Example
5. Bitwise Operators
Bitwise operators work at the binary level and are useful for low-level programming.
| Operator | Description |
|---|---|
| & | AND |
| | | OR |
| ^ | XOR |
| << | Left shift |
| >> | Right shift |
Example
Go Arithmetic Operators
Arithmetic operators in Go are used to perform basic mathematical calculations such as addition, subtraction, multiplication, and more. These operators are essential for building expressions and handling numeric data in your programs.
List of Arithmetic Operators in Go
| Operator | Name | Description | Example |
|---|---|---|---|
| + | Addition | Adds two values | x + y |
| - | Subtraction | Subtracts one value from another | x - y |
| * | Multiplication | Multiplies two values | x * y |
| / | Division | Divides one value by another | x / y |
| % | Modulus | Returns the remainder of division | x % y |
| ++ | Increment | Increases a variable’s value by 1 | x++ |
| -- | Decrement | Decreases a variable’s value by 1 | x-- |
Example: Using Arithmetic Operators in Go
Important Notes
1. Integer Division
When both operands are integers, division returns an integer result (decimal part is truncated):
To get a floating-point result, use float64:
2. Increment and Decrement Restrictions
In Go, ++ and --:
- Can only be used as statements, not expressions
- Cannot be used inside other operations like y = x++
Go Assignment Operators
Assignment operators in Go are used to assign values to variables and update them efficiently. They are fundamental to managing data and performing operations in a concise way.
What Are Assignment Operators?
An assignment operator assigns a value to a variable. The most basic operator is =.
Example: Basic Assignment
Compound Assignment Operators
Go provides compound assignment operators that combine arithmetic or bitwise operations with assignment. These operators make code shorter and more readable.
Example: Using Compound Assignment
List of Assignment Operators in Go
| Operator | Example | Equivalent Expression |
|---|---|---|
| = | x = 5 | x = 5 |
| += | x += 3 | x = x + 3 |
| -= | x -= 3 | x = x - 3 |
| *= | x *= 3 | x = x * 3 |
| /= | x /= 3 | x = x / 3 |
| %= | x %= 3 | x = x % 3 |
| &= | x &= 3 | x = x & 3 |
| |= | x |= 3 | x = x | 3 |
| ^= | x ^= 3 | x = x ^ 3 |
| >>= | x >>= 2 | x = x >> 2 |
| <<= | x <<= 2 | x = x << 2 |
Example: Multiple Assignment Operations
Bitwise Assignment Example
Key Benefits of Assignment Operators
- Reduce code verbosity
- Improve readability
- Combine operations efficiently
- Commonly used in loops and calculations
Go Comparison Operators
Comparison operators in Go are used to compare two values or expressions. These operators are essential for decision-making in programs, especially when working with conditions such as if, else, and loops.
What Are Comparison Operators?
A comparison operator evaluates the relationship between two values and returns a boolean result:
- true → if the condition is satisfied
- false → if the condition is not satisfied
Example: Basic Comparison
List of Comparison Operators in Go
| Operator | Name | Example |
|---|---|---|
| == | Equal to | x == y |
| != | Not equal to | x != y |
| > | Greater than | x > y |
| < | Less than | x < y |
| >= | Greater than or equal to | x >= y |
| <= | Less than or equal to | x <= y |
Example: Using Multiple Comparison Operators
Using Comparison Operators in Conditions
Go Logical Operators
Logical operators are essential in programming for combining and evaluating multiple conditions. They allow you to build complex decision-making logic by working with boolean values (true or false).
These operators are commonly used in conditional statements such as if, while, and for loops.
Types of Logical Operators
Most programming languages, including C, C++, Java, and Go, support the following three logical operators:
| Operator | Name | Description |
|---|---|---|
| && | Logical AND | Returns true only if both conditions are true |
| || | Logical OR | Returns true if at least one condition is true |
| ! | Logical NOT | Reverses the result (true becomes false, false becomes true) |
1. Logical AND (&&)
The Logical AND operator evaluates to true only when all conditions are true.
Example:
Explanation:
- The condition checks whether the person is at least 18 years old and has an ID.
- Both conditions must be true for access to be granted.
2. Logical OR (||)
The Logical OR operator returns true if any one of the conditions is true.
Example:
Explanation:
- The discount is applied if the user is a member or has a coupon.
- Only one condition needs to be true.
3. Logical NOT (!)
The Logical NOT operator is used to invert a condition.
Example:
Explanation:
- !isRaining means “it is NOT raining.”
- The result is reversed: 0 (false) becomes true.
Combining Logical Operators
Logical operators can be combined to form more complex conditions.
Example:
Explanation:
- A student is eligible if:
- They have good marks and attendance, or
- They have exceptionally high marks.
Go Bitwise Operators
Bitwise operators allow you to perform operations directly on the binary (bit-level) representation of integers. These operators are commonly used in low-level programming, performance optimization, embedded systems, and tasks involving flags or masks.
Unlike logical operators, which work with boolean values, bitwise operators manipulate individual bits (0s and 1s) within a number.
Why Use Bitwise Operators?
Bitwise operations are:
- Fast and efficient (executed at the hardware level)
- Useful for memory optimization
- Widely used in system programming, encryption, and data compression
Types of Bitwise Operators
| Operator | Name | Description |
|---|---|---|
| & | Bitwise AND | Sets each bit to 1 if both corresponding bits are 1 |
| | | Bitwise OR | Sets each bit to 1 if at least one bit is 1 |
| ^ | Bitwise XOR | Sets each bit to 1 if the bits are different |
| << | Left Shift | Shifts bits to the left, filling with 0s from the right |
| >> | Right Shift | Shifts bits to the right (preserves sign in signed types) |
1. Bitwise AND (&)
The AND operator compares each bit of two numbers and returns 1 only if both bits are 1.
Example:
Explanation:
110 (6) & 011 (3) ------ 010 (2)
2. Bitwise OR (|)
The OR operator returns 1 if at least one of the bits is 1.
Example:
Explanation:
101 (5) | 010 (2) ------ 111 (7)
3. Bitwise XOR (^)
The XOR (exclusive OR) operator returns 1 only when the bits are different.
Example:
Explanation:
111 (7) ^ 100 (4) ------ 011 (3)
4. Left Shift (<<)
The left shift operator moves bits to the left and fills the right side with zeros. Each shift left effectively multiplies the number by 2.
Example:
Explanation:
00000100 (4) << 2 ---------- 00010000 (16)
5. Right Shift (>>)
The right shift operator moves bits to the right. For signed integers, the leftmost bit (sign bit) is preserved.
Example:
Explanation:
00010000 (16) >> 2 ---------- 00000100 (4)
Practical Use Case: Bit Masking
Bitwise operators are often used to set, clear, and check specific bits.
