Go Operators
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

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.

Example: Operations with Variables

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 Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus (remainder) a % b

Example

Addition: 26 Subtraction: 14 Multiplication: 120 Division: 3 Remainder: 2

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

Final Value: 30

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

x > y: true x == y: false x != y: true

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

Eligible: true

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

AND: 1 OR: 7 XOR: 6 Left Shift: 10 Right Shift: 2

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

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

Important Notes

1. Integer Division

When both operands are integers, division returns an integer result (decimal part is truncated):

result := 7 / 2 // result = 3

To get a floating-point result, use float64:

result := float64(7) / float64(2) // result = 3.5

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

Score: 95

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

Final Points: 60

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

Final Value: 2

Bitwise Assignment Example

Updated Flags: 2

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
Note: In Go, the result is strictly true or false (not 1 or 0 as in some other languages).

Example: Basic Comparison

Is a greater than b? true

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

Passed: true Perfect score: false Needs improvement: false

Using Comparison Operators in Conditions

Eligible to vote

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:

Entry allowed.

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:

Discount applied.

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:

You can go outside.

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:

Eligible for reward.

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:

Bitwise AND result: 2

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:

Bitwise OR result: 7

Explanation:

  101  (5)
| 010  (2)
------
  111  (7)

3. Bitwise XOR (^)

The XOR (exclusive OR) operator returns 1 only when the bits are different.

Example:

Bitwise XOR result: 3

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:

Left shift result: 16

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:

Right shift result: 4

Explanation:

  00010000  (16)
>> 2
----------
  00000100  (4)

Practical Use Case: Bit Masking

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

Example:

Bit 1 is ON Final flags value: 0


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