Operators
## Learning Objectives
- Master arithmetic operators
- Understand comparison operators
- Learn logical operators
- Work with bitwise operators
- Understand operator precedence
## Arithmetic Operators
### Basic Operations
```go
a, b := 10, 3
fmt.Println(a + b) // 13 (addition)
fmt.Println(a - b) // 7 (subtraction)
fmt.Println(a * b) // 30 (multiplication)
fmt.Println(a / b) // 3 (integer division)
fmt.Println(a % b) // 1 (modulus/remainder)
```
### Division Behavior
```go
// Integer division truncates toward zero
fmt.Println(10 / 3) // 3, not 3.333...
// For floating-point division
fmt.Println(10.0 / 3.0) // 3.333...
```
### Increment/Decrement
```go
x := 5
x++ // x = 6 (equivalent to x += 1)
x-- // x = 5 (equivalent to x -= 1)
// Pre/post behavior is the same - no value returned
fmt.Println(x) // 5
```
### Compound Assignment
```go
x := 10
x += 5 // x = 15
x -= 3 // x = 12
x *= 2 // x = 24
x /= 4 // x = 6
x %= 5 // x = 1
```
## Comparison Operators
### Relational Operators
```go
a, b := 5, 10
fmt.Println(a == b) // false (equal)
fmt.Println(a != b) // true (not equal)
fmt.Println(a < b) // true (less than)
fmt.Println(a > b) // false (greater than)
fmt.Println(a <= b) // true (less or equal)
fmt.Println(a >= b) // false (greater or equal)
```
### Important Note
```go
// = is assignment
// == is comparison
x := 5
if x == 5 { // Correct: compares x to 5
fmt.Println("x is 5")
}
```
## Logical Operators
### AND, OR, NOT
```go
a, b := true, false
fmt.Println(a && b) // false (AND - both must be true)
fmt.Println(a || b) // true (OR - one must be true)
fmt.Println(!a) // false (NOT - inverts)
```
### Short-Circuit Evaluation
```go
x := 0
// && stops if first is false
if x != 0 && y/x > 2 { // Safe - won't divide by zero
fmt.Println("safe")
}
// || stops if first is true
if obj == nil || obj.isValid() { // Safe - won't call on nil
fmt.Println("safe")
}
```
### Truth Table
| A | B | A && B | A \|\| B | !A |
|---|---|--------|----------|-----|
| true | true | true | true | false |
| true | false | false | true | false |
| false | true | false | true | true |
| false | false | false | false | true |
## Bitwise Operators
### Binary Operations
```go
a, b := 5, 3 // 101 and 011 in binary
fmt.Println(a & b) // 1 (AND: 101 & 011 = 001)
fmt.Println(a | b) // 7 (OR: 101 | 011 = 111)
fmt.Println(a ^ b) // 6 (XOR: 101 ^ 011 = 110)
fmt.Println(^a) // -6 (NOT: ^101 = ...010 = -6)
fmt.Println(a << 1) // 10 (left shift: 101 << = 1010)
fmt.Println(a >> 1) // 2 (right shift: 101 >> = 10)
```
### Bitwise Assignment
```go
x := 10
x &= 3 // x = x & 3
x |= 3 // x = x | 3
x ^= 3 // x = x ^ 3
x <<= 2 // x = x << 2
x >>= 1 // x = x >> 1
```
### Common Uses
```go
// Check if bit is set
flags := 0b1010 // 10 in decimal
if flags & 0b1000 != 0 {
fmt.Println("Bit 3 is set")
}
// Set a bit
flags = flags | 0b0001
// Toggle a bit
flags = flags ^ 0b0100
// Clear a bit
flags = flags &^ 0b0100
```
## Address Operators
### Pointer Operations
```go
x := 10
p := &x // Get address of x
fmt.Println(p) // Memory address (e.g., 0xc00000a008)
fmt.Println(*p) // 10 (dereference)
*p = 20 // Change value through pointer
fmt.Println(x) // 20
```
### New Operator
```go
p := new(int) // Creates pointer to int with zero value
fmt.Println(*p) // 0
```
## Channel Operators
### Channel Creation and Operations
```go
ch := make(chan int)
// Send and receive
ch <- 10 // Send 10 to channel
value := <-ch // Receive from channel
```
## Operator Precedence
### Highest to Lowest
| Priority | Operators |
|----------|-------------------|
| 1 | `*`, `/`, `%`, `<<`, `>>`, `&`, `&^` |
| 2 | `+`, `-`, `\|`, `^` |
| 3 | `==`, `!=`, `<`, `<=`, `>`, `>=` |
| 4 | `<-` (channel) |
| 5 | `&&` |
| 6 | `\|\|` |
### Use Parentheses
```go
// Clear precedence
if (a > b) && (c < d) {
fmt.Println("Both conditions true")
}
// Same result, less clear
if a > b && c < d {
fmt.Println("Both conditions true")
}
```
## Type Assertion Operators
### Type Switch
```go
var i interface{} = "hello"
switch v := i.(type) {
case int:
fmt.Printf("Integer: %d\n", v)
case string:
fmt.Printf("String: %s\n", v)
default:
fmt.Printf("Unknown type\n")
}
```
### Type Assertion
```go
var i interface{} = "hello"
s := i.(string) // Panics if not string
s, ok := i.(string) // Safe: ok is false if not string
```
## Summary
- Arithmetic: `+`, `-`, `*`, `/`, `%`
- Increment/Decrement: `++`, `--`
- Comparison: `==`, `!=`, `<`, `>`, `<=`, `>=`
- Logical: `&&`, `||`, `!`
- Bitwise: `&`, `|`, `^`, `~`, `<<`, `>>`
- Assignment: `=`, `+=`, `-=`, etc.
- Pointer: `&` (address), `*` (dereference)
- Channel: `<-` (send/receive)
- Use parentheses to clarify precedence
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →