Go Operators
Performing operations and calculations in Go
⚡ What are Go Operators?
Go operators are symbols that perform operations on variables and values. They include arithmetic operators for math, comparison operators for conditions, and logical operators for boolean operations.
package main
import "fmt"
func main() {
x := 10
y := 3
fmt.Println("Addition:", x + y) // 13
fmt.Println("Greater than:", x > y) // true
fmt.Println("Logical AND:", true && false) // false
}
Output:
Addition: 13 Greater than: true Logical AND: false
Types of Go Operators
Arithmetic
Math operations like +, -, *, /
x + y // Addition
x - y // Subtraction
x * y // Multiplication
Comparison
Compare values with ==, !=, <, >
x == y // Equal
x != y // Not equal
x > y // Greater than
Logical
Boolean operations with &&, ||, !
a && b // AND
a || b // OR
!a // NOT
Assignment
Assign values with =, +=, -=
x = 10 // Assign
x += 5 // Add and assign
x++ // Increment
🔹 Arithmetic Operators
Basic math operations in Go:
package main
import "fmt"
func main() {
a := 15
b := 4
fmt.Println("Numbers:", a, "and", b)
fmt.Println("Addition (a + b):", a + b) // 19
fmt.Println("Subtraction (a - b):", a - b) // 11
fmt.Println("Multiplication (a * b):", a * b) // 60
fmt.Println("Division (a / b):", a / b) // 3 (integer division)
fmt.Println("Modulus (a % b):", a % b) // 3 (remainder)
// Float division
var x float64 = 15
var y float64 = 4
fmt.Println("Float division:", x / y) // 3.75
}
Output:
Numbers: 15 and 4 Addition (a + b): 19 Subtraction (a - b): 11 Multiplication (a * b): 60 Division (a / b): 3 Modulus (a % b): 3 Float division: 3.75
🔹 Comparison Operators
Comparing values and getting boolean results:
package main
import "fmt"
func main() {
x := 10
y := 20
z := 10
fmt.Println("x =", x, ", y =", y, ", z =", z)
fmt.Println("x == y:", x == y) // false (equal)
fmt.Println("x != y:", x != y) // true (not equal)
fmt.Println("x < y:", x < y) // true (less than)
fmt.Println("x > y:", x > y) // false (greater than)
fmt.Println("x <= z:", x <= z) // true (less than or equal)
fmt.Println("x >= z:", x >= z) // true (greater than or equal)
// String comparison
name1 := "Alice"
name2 := "Bob"
fmt.Println("Alice == Bob:", name1 == name2) // false
}
Output:
x = 10 , y = 20 , z = 10 x == y: false x != y: true x < y: true x > y: false x <= z: true x >= z: true Alice == Bob: false
🔹 Logical Operators
Combining boolean values with logical operations:
package main
import "fmt"
func main() {
age := 25
hasLicense := true
hasInsurance := false
// Logical AND (&&) - both must be true
canDrive := age >= 18 && hasLicense
fmt.Println("Can drive:", canDrive) // true
// Logical OR (||) - at least one must be true
needsDocuments := !hasLicense || !hasInsurance
fmt.Println("Needs documents:", needsDocuments) // true
// Logical NOT (!) - reverses boolean value
isMinor := !(age >= 18)
fmt.Println("Is minor:", isMinor) // false
// Complex logical expression
canRentCar := age >= 21 && hasLicense && hasInsurance
fmt.Println("Can rent car:", canRentCar) // false
}
Output:
Can drive: true Needs documents: true Is minor: false Can rent car: false
🔹 Assignment Operators
Different ways to assign and modify variables:
package main
import "fmt"
func main() {
// Basic assignment
x := 10
fmt.Println("Initial x:", x)
// Compound assignment operators
x += 5 // Same as: x = x + 5
fmt.Println("After x += 5:", x) // 15
x -= 3 // Same as: x = x - 3
fmt.Println("After x -= 3:", x) // 12
x *= 2 // Same as: x = x * 2
fmt.Println("After x *= 2:", x) // 24
x /= 4 // Same as: x = x / 4
fmt.Println("After x /= 4:", x) // 6
x %= 4 // Same as: x = x % 4
fmt.Println("After x %= 4:", x) // 2
// Increment and decrement
x++ // Same as: x = x + 1
fmt.Println("After x++:", x) // 3
x-- // Same as: x = x - 1
fmt.Println("After x--:", x) // 2
}
Output:
Initial x: 10 After x += 5: 15 After x -= 3: 12 After x *= 2: 24 After x /= 4: 6 After x %= 4: 2 After x++: 3 After x--: 2
🔹 Operator Precedence
Understanding the order of operations:
package main
import "fmt"
func main() {
// Operator precedence (highest to lowest):
// 1. *, /, %
// 2. +, -
// 3. ==, !=, <, <=, >, >=
// 4. &&
// 5. ||
result1 := 2 + 3 * 4 // 14 (not 20)
result2 := (2 + 3) * 4 // 20 (parentheses first)
result3 := 10 > 5 && 3 < 7 // true
result4 := 10 > 5 || 3 > 7 // true
fmt.Println("2 + 3 * 4 =", result1)
fmt.Println("(2 + 3) * 4 =", result2)
fmt.Println("10 > 5 && 3 < 7 =", result3)
fmt.Println("10 > 5 || 3 > 7 =", result4)
// Complex expression
x := 5
y := 10
z := 15
complex := x + y * z / 5 - 2
fmt.Println("5 + 10 * 15 / 5 - 2 =", complex) // 33
}
Output:
2 + 3 * 4 = 14 (2 + 3) * 4 = 20 10 > 5 && 3 < 7 = true 10 > 5 || 3 > 7 = true 5 + 10 * 15 / 5 - 2 = 33