Operators in Go
Operators are symbols that perform operations on variables and values. Go supports various types of operators used in expressions.
1. Arithmetic Operators
Operator |
Description |
Example |
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
2. Relational (Comparison) Operators
Operator |
Description |
Example |
== | Equal to | a == b |
!= | Not equal to | a != b |
< | Less than | a < b |
<= | Less than or equal to | a <= b |
> | Greater than | a > b |
>= | Greater than or equal to | a >= b |
3. Logical Operators
Operator |
Description |
Example |
&& | Logical AND | a && b |
|| | Logical OR | a || b |
! | Logical NOT | !a |
4. Assignment Operators
Operator |
Description |
Example |
= | Assign | a = b |
+= | Add and assign | a += b |
-= | Subtract and assign | a -= b |
*= | Multiply and assign | a *= b |
/= | Divide and assign | a /= b |
%= | Modulus and assign | a %= b |
5. Other Operators
&
: Address of a variable (pointer).
*
: Dereference pointer to get value.
<<
: Left shift.
>>
: Right shift.
^
: Bitwise XOR (exclusive OR).
^=
: Bitwise XOR and assign.
Note: Go does not support the increment (++) or decrement (--) operators as expressions; they can only be used as standalone statements.