Operators in Go
Operators in Go are special symbols used to perform operations on variables and values. They allow you to calculate numbers, compare values, combine conditions, and modify data.
In real applications, operators are used everywhere — from calculating totals, checking conditions, looping through data, to building APIs and business logic.
What Is an Operator?
An operator performs an action on one or more operands (values or variables). For example, adding two numbers or comparing two values.
Go provides a clear and minimal set of operators that are easy to read and maintain.
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
Assume we are working with product prices in an application:
package main
import "fmt"
func main() {
price := 120
tax := 30
fmt.Println(price + tax) // addition
fmt.Println(price - tax) // subtraction
fmt.Println(price * 2) // multiplication
fmt.Println(price / 3) // division
fmt.Println(price % 7) // remainder
}
Explanation:
+adds values-subtracts values*multiplies values/divides values%returns the remainder
Assignment Operators
Assignment operators are used to assign or update values stored in variables.
They are commonly used when updating counters, balances, or scores.
package main
import "fmt"
func main() {
balance := 1000
balance += 200
balance -= 150
balance *= 2
balance /= 5
fmt.Println(balance)
}
Here:
+=adds and assigns-=subtracts and assigns*=multiplies and assigns/=divides and assigns
Comparison Operators
Comparison operators compare two values and return a boolean result:
true or false.
These operators are critical for conditions and decision-making.
package main
import "fmt"
func main() {
age := 21
fmt.Println(age == 18)
fmt.Println(age != 18)
fmt.Println(age > 18)
fmt.Println(age < 30)
fmt.Println(age >= 21)
fmt.Println(age <= 25)
}
These comparisons are often used in login checks, eligibility rules, and validations.
Logical Operators
Logical operators combine multiple conditions. They are widely used in authentication and filtering logic.
package main
import "fmt"
func main() {
age := 25
hasID := true
fmt.Println(age >= 18 && hasID)
fmt.Println(age < 18 || hasID)
fmt.Println(!hasID)
}
Logical operators:
&&→ AND (both conditions must be true)||→ OR (any one condition is true)!→ NOT (reverses the condition)
Increment and Decrement Operators
These operators increase or decrease a value by one. They are commonly used in counters and loops.
package main
import "fmt"
func main() {
count := 10
count++
fmt.Println(count)
count--
fmt.Println(count)
}
Note: In Go, ++ and -- are statements, not expressions.
Bitwise Operators
Bitwise operators work directly on binary values. They are used in low-level programming, performance optimization, and systems work.
package main
import "fmt"
func main() {
a := 6 // 110
b := 3 // 011
fmt.Println(a & b)
fmt.Println(a | b)
fmt.Println(a ^ b)
}
Bitwise operators include AND (&), OR (|), and XOR (^).
Operator Precedence
Operator precedence determines the order in which operations are evaluated.
result := 10 + 5 * 2
fmt.Println(result)
Multiplication happens before addition, so the result is 20, not 30.
Real-World Example
Let’s calculate a final bill amount with tax and discount.
package main
import "fmt"
func main() {
price := 1500
discount := 200
taxRate := 0.10
finalPrice := (price - discount)
tax := float64(finalPrice) * taxRate
total := float64(finalPrice) + tax
fmt.Println("Total amount:", total)
}
This example shows how multiple operators work together in real applications.
Why Operators Matter in Go
Operators form the foundation of all Go programs.
- They control calculations
- They drive decisions
- They enable logic and flow
What’s Next?
In the next lesson, you will learn how to take input from users and display output using Go’s input and output techniques.