Operators in Go are categorized into several distinct types, each performing specific operations or computations. The following are common categories of operators in Go:
-
Arithmetic Operators: These operators perform basic mathematical operations.
+(addition)-(subtraction)*(multiplication)/(division)%(modulo)
For example, calculating the sum of two numbers:
a + b. -
Comparison Operators: These operators compare two values.
==(equal to)!=(not equal to)<(less than)>(greater than)<=(less than or equal to)>=(greater than or equal to)
For example, checking if two numbers are equal:
if a == b { ... }. -
Logical Operators: Used for combining multiple boolean expressions.
&&(logical AND)||(logical OR)!(logical NOT)
For example, checking if two conditions are both satisfied:
if a > 10 && b < 20 { ... }. -
Bitwise Operators: Operators that operate at the bit level.
&(bitwise AND)|(bitwise OR)^(bitwise XOR)&^(bit clear)<<(left shift)>>(right shift)
For example, shifting a number left by two bits:
a << 2. -
Assignment Operators: Used for assignment.
=(simple assignment)+=(add and assign)-=(subtract and assign)*=(multiply and assign)/=(divide and assign)%=(modulo and assign)<<=(left shift and assign)>>=(right shift and assign)&=(bitwise AND and assign)|=(bitwise OR and assign)^=(bitwise XOR and assign)
For example, incrementing a variable and assigning:
a += 1. -
Other Operators:
&(address operator)*(pointer dereference operator)
For example, obtaining the address of a variable:
ptr := &a.
The above are the common categories of operators in Go. Using these operators, programmers can perform various logical and computational operations to achieve complex functionalities and algorithms.