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.
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
Important Notes:
-
Integer Division: When both operands are integers,
division returns an integer result (decimal part is truncated). E.g.,
7 / 2results in3. To get a float, usefloat64(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
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
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 (&&)
Example: Logical OR (||)
Example: Combining Logical Operators
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
Example: Bit Shift and Bit Masking
Bitwise operators are often used to set, clear, and check specific bits using shifts.
