Go Operators: A Comprehensive Guide
Operators are the building blocks of expressions in Go, allowing you to manipulate values and perform calculations. This guide provides a detailed overview of different operator types and their usage.
Arithmetic Operators
Used for basic mathematical operations:
- Addition (+): Adds two operands.
- Subtraction (-): Subtracts the second operand from the first.
- Multiplication (*): Multiplies two operands.
- Division (/): Divides the first operand by the second.
- Modulus (%): Returns the remainder of the division.
- Increment (++): Increases the value of a variable by 1.
- Decrement (--): Decreases the value of a variable by 1.
Example:
Syntax
package main
import "fmt"
func main() {
x := 10
y := 3
sum := x + y
difference := x - y
product := x * y
quotient := x / y
remainder := x % y
fmt.Println(sum, difference, product, quotient, remainder)
}
Operator Table
Operator | Description | Syntax |
---|---|---|
+ | Addition. Adds two values. | x + y |
- | Subtraction. Subtracts the second operand from the first. | x - y |
* | Multiplication. | x * y |
/ | Division. | x / y |
% | Modulus. Gives the remainder after division. | x % y |
++ | Increment operator. Increments the value by 1. | x++ |
-- | Decrement operator. Decrements the value by 1. | x-- |
Comparison Operators
Used to compare two values and return a boolean result:
- Equal to (==): Checks if two operands are equal.
- Not equal to (!=): Checks if two operands are not equal.
- Less than (<): Checks if the left operand is less than the right operand.
- Greater than (>): Checks if the left operand is greater than the right operand.
- Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.
- Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
Example:
Syntax
package main
import "fmt"
func main() {
x := 10
y := 5
fmt.Println(x == y) // false
fmt.Println(x != y) // true
fmt.Println(x < y) // false
fmt.Println(x > y) // true
}
Operator Table
Operator | Description | Syntax |
---|---|---|
== | Equal | x == y |
!= | Not Equal | x != y |
< | Less than | x < y |
<= | Less than or equal | x <= y |
> | Greater than | x > y |
>= | Greater than or equal | x >= y |
Logical Operators
Used to combine boolean expressions:
- Logical AND (&&): Returns true if both operands are true.
- Logical OR (||): Returns true if at least one operand is true.
- Logical NOT (!): Reverses the logical state of its operand.
Example:
Syntax
package main
import "fmt"
func main() {
x := true
y := false
fmt.Println(x && y) // false
fmt.Println(x || y) // true
fmt.Println(!x) // false
}
Operator Table
Operator | Description | Syntax |
---|---|---|
&& | Logical AND | expression1 && expression2 |
|| | Logical OR | exp1 || exp2 |
! | Logical NOT | !exp |
Bitwise Operators
Used to perform operations on individual bits of integer values:
- Bitwise AND (&): Performs a bitwise AND operation.
- Bitwise OR (|): Performs a bitwise OR operation.
- Bitwise XOR (^): Performs a bitwise XOR operation.
- Left shift (<<): Shifts bits to the left.
- Right shift (>>): Shifts bits to the right.
Example:
Syntax
package main
import "fmt"
func main() {
x := 5 // Binary: 00000101
y := 3 // Binary: 00000011
z := x & y
fmt.Println(z) // 1 (Binary: 00000001)
}
Operator Table
Operator | Description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
<< | Left shift |
>> | Right shift |
Assignment Operators
Used to assign values to variables:
- Equal (=): Assigns the value of the right operand to the left operand.
- Addition assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.
- Subtraction assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
- Multiplication assignment (*=): Multiplies the left operand by the right operand and assigns the result to the left operand.
- Division assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand.
- Modulus assignment (%=): Calculates the modulus of the left operand and the right operand and assigns the result to the left operand.
Example:
Syntax
package main
import "fmt"
func main() {
x := 5
x += 3 // Equivalent to x = x + 3
fmt.Println(x) // 8
}
Operator Table
Operator | Description | Syntax |
---|---|---|
= | Assign value | x = y |
+= | Add and assign | x += y (same as x = x + y) |
-= | Subtract and assign | x -= y (same as x = x - y) |
*= | Multiply and assign | x *= y (same as x = x * y) |
/= | Divide and assign | x /= y (same as x = x / y) |
%= | Divide and assign modulus | x %= y (same as x = x % y) |
By understanding these operators, you can effectively manipulate data and perform calculations in your Go programs.