Operators
Start with Go Operators
Operators combine values to create new values or test conditions. Go provides arithmetic, comparison, logical, bitwise and assignment operators.
What you’ll learn
- Evaluate arithmetic and comparison expressions.
- Use logical operators for conditions.
- Differentiate integer division from floating-point division.
Understand the idea
Arithmetic operators include +, -, *, / and %. Dividing two integers uses integer division, so 7 / 2 evaluates to 3. Comparison operators such as < and == produce booleans.
Logical operators &&, || and ! combine boolean expressions. Parentheses improve clarity when operators have different precedence. Assignment shorthands such as += update an existing variable.
Visualize how it works
Follow this diagram before executing the example. Check where a value or decision changes what runs next.
Run a complete Go example
This example is a complete program. Save it as main.go or paste it into the Go Playground.
Go code
package main
import "fmt"
func main() {
a, b := 7, 2
fmt.Println("Division:", a/b)
fmt.Println("Remainder:", a%b)
age := 19
hasID := true
fmt.Println("Can enter:", age >= 18 && hasID)
a += 3
fmt.Println("Updated a:", a)
}
Output
Division: 3
Remainder: 1
Can enter: true
Updated a: 10Trace what the program does
The integer division result is 3 with remainder 1. The condition evaluates both age >= 18 and hasID, so it prints true. The compound assignment changes a from 7 to 10.
Avoid common mistakes
- Do not use
=where equality comparison==is intended. - Go does not permit arbitrary integers directly as conditions; conditions must evaluate to booleans.
Try it yourself
Change age to 16, then use || in a separate eligibility rule. Explain which combinations make the results true.
Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.
Check your understanding
What you should remember
You have practiced Go Operators in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.
Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.
Reaching the recap and completing the quizzes are separate milestones.