Go Operators

Go Operators

Kishore V


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

Result: 20

Using Operators with Variables

Operators can also work with variables and expressions, not just fixed values.

Total: 75 Double Total: 150

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 Name Description Example
+ Addition Adds two values a + b
- Subtraction Subtracts one value from another a - b
* Multiplication Multiplies two values a * b
/ Division Divides one value by another a / b
% Modulus Returns the remainder of division a % b
++ Increment Increases a variable’s value by 1 a++
-- Decrement Decreases a variable’s value by 1 a--

Example: Arithmetic Operators

Addition: 22 Subtraction: 14 Multiplication: 72 Division: 4 Modulus: 2 After Increment: 6 After Decrement: 5

Important Notes:

  • Integer Division: When both operands are integers, division returns an integer result (decimal part is truncated). E.g., 7 / 2 results in 3. To get a float, use float64(7) / float64(2).
  • Increment/Decrement: In Go, ++ and -- can only be used as statements, not expressions (e.g., y = x++ is invalid).

2. Assignment Operators

Assignment operators are used to assign values to variables and update them efficiently.

Operator Example Equivalent To
= 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

Example: Compound Assignment

Final Value: 2

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 than or equal to a >= b
<= Less than or equal to a <= b

Example: Comparison in Conditions

Passed: true Perfect score: false Needs improvement: false

4. Logical Operators

Logical operators are essential for combining and evaluating multiple conditions. They build complex decision-making logic using boolean values.

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)

Example: Logical AND (&&)

Entry allowed.

Example: Logical OR (||)

Discount applied.

Example: Combining Logical Operators

Eligible for reward.

5. Bitwise Operators

Bitwise operators allow you to perform operations directly on the binary (bit-level) representation of integers. They are fast, memory-efficient, and widely used in system programming and data compression.

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 of the bits 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

Example: Bitwise AND, OR, XOR

Bitwise AND result: 2 Bitwise OR result: 7 Bitwise XOR result: 5

Example: Bit Shift and Bit Masking

Bitwise operators are often used to set, clear, and check specific bits using shifts.

Left shift result: 16 Bit 1 is ON Final flags value: 0


Our website uses cookies to enhance your experience. Learn More
Accept !