Welcome to The Coding College!
Operators are essential in any programming language as they perform actions like calculations, comparisons, logical evaluations, and more. In Go (Golang), operators are categorized based on their functionality. This guide will cover all types of operators in Go with syntax, examples, and use cases.
What Are Operators in Go?
An operator is a symbol or keyword used to perform operations on variables and values. For example:
result := 5 + 3 // '+' is an operator for addition
Types of Operators in Go
Go provides the following categories of operators:
- Arithmetic Operators
- Relational (Comparison) Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Miscellaneous Operators
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations.
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 6 / 3 | 2 |
% | Modulus (remainder) | 5 % 3 | 2 |
Example: Arithmetic Operators
package main
import "fmt"
func main() {
a, b := 10, 3
fmt.Println("Addition:", a+b) // Output: 13
fmt.Println("Subtraction:", a-b) // Output: 7
fmt.Println("Multiplication:", a*b) // Output: 30
fmt.Println("Division:", a/b) // Output: 3
fmt.Println("Modulus:", a%b) // Output: 1
}
2. Relational (Comparison) Operators
Relational operators compare two values and return a boolean (true
or false
).
Operator | Description | Example | Result |
---|---|---|---|
== | Equal to | 5 == 5 | true |
!= | Not equal to | 5 != 3 | true |
< | Less than | 3 < 5 | true |
<= | Less than or equal to | 5 <= 5 | true |
> | Greater than | 5 > 3 | true |
>= | Greater than or equal to | 5 >= 3 | true |
Example: Relational Operators
package main
import "fmt"
func main() {
x, y := 10, 20
fmt.Println("x == y:", x == y) // Output: false
fmt.Println("x != y:", x != y) // Output: true
fmt.Println("x < y:", x < y) // Output: true
fmt.Println("x > y:", x > y) // Output: false
}
3. Logical Operators
Logical operators are used to combine or invert boolean conditions.
Operator | Description | Example | Result |
---|---|---|---|
&& | Logical AND | true && false | false |
` | ` | Logical OR | |
! | Logical NOT | !true | false |
Example: Logical Operators
package main
import "fmt"
func main() {
a, b := true, false
fmt.Println("a && b:", a && b) // Output: false
fmt.Println("a || b:", a || b) // Output: true
fmt.Println("!a:", !a) // Output: false
}
4. Bitwise Operators
Bitwise operators perform operations on binary representations of numbers.
Operator | Description | Example | Result |
---|---|---|---|
& | Bitwise AND | 5 & 3 | 1 |
` | ` | Bitwise OR | `5 |
^ | Bitwise XOR | 5 ^ 3 | 6 |
<< | Left shift | 2 << 1 | 4 |
>> | Right shift | 4 >> 1 | 2 |
Example: Bitwise Operators
package main
import "fmt"
func main() {
x, y := 5, 3
fmt.Println("x & y:", x&y) // Output: 1
fmt.Println("x | y:", x|y) // Output: 7
fmt.Println("x ^ y:", x^y) // Output: 6
fmt.Println("x << 1:", x<<1) // Output: 10
fmt.Println("x >> 1:", x>>1) // Output: 2
}
5. Assignment Operators
Assignment operators are used to assign and modify values of variables.
Operator | Description | Example | Equivalent |
---|---|---|---|
= | Assign | x = 5 | x = 5 |
+= | Add and assign | x += 3 | x = x + 3 |
-= | Subtract and assign | x -= 3 | x = x - 3 |
*= | Multiply and assign | x *= 3 | x = x * 3 |
/= | Divide and assign | x /= 3 | x = x / 3 |
%= | Modulus and assign | x %= 3 | x = x % 3 |
Example: Assignment Operators
package main
import "fmt"
func main() {
x := 10
x += 5
fmt.Println("x += 5:", x) // Output: 15
x *= 2
fmt.Println("x *= 2:", x) // Output: 30
}
6. Miscellaneous Operators
Address Operator (&
)
Used to get the memory address of a variable.
x := 10
fmt.Println(&x) // Output: Memory address of x
Dereference Operator (*
)
Used to access the value at a memory address.
ptr := &x
fmt.Println(*ptr) // Output: 10
Conclusion
Operators are the backbone of any program, enabling logical, arithmetic, and data manipulation. By mastering operators in Go, you can write concise, efficient, and powerful code.