Stage 1 · Code
Variables, Types & Control Flow
Operators & Expressions
Combine values with arithmetic, comparison, logical, and string operators — and avoid the integer division trap.
Arithmetic Operators
Go supports the standard arithmetic operators for numeric types. A key rule: all operands must be the same type. You cannot add an int and a float64 without converting one first.
This is a common beginner mistake. 10 / 3 is 3, not 3.33. If you need the real division, convert at least one operand: float64(10) / float64(3) or 10.0 / 3.0.
Comparison Operators
Comparison operators compare two values of the same type and always return a bool. You cannot use == between two values of different types without an explicit conversion.
| Operator | Meaning | Example |
|---|---|---|
| == | Equal to | 5 == 5 → true |
| != | Not equal to | 5 != 3 → true |
| < | Less than | 3 < 5 → true |
| <= | Less than or equal | 5 <= 5 → true |
| > | Greater than | 7 > 3 → true |
| >= | Greater than or equal | 3 >= 7 → false |
Logical Operators
Logical operators combine boolean expressions. && is AND, || is OR, ! is NOT. Both && and || short-circuit: if the result is determined by the left operand, the right operand is never evaluated.
String Operators
Go strings support + for concatenation and all six comparison operators. For building strings in a loop or from many parts, prefer strings.Builder or fmt.Sprintf over repeated +.
Operator Precedence
Go operators have a defined precedence order — similar to standard algebra. Multiplication and division bind tighter than addition and subtraction. When in doubt, use parentheses to make order explicit.
Operator precedence rules can surprise even experienced developers. Explicit parentheses cost nothing in performance and document your intent clearly. A compiler optimizes them away; a reader relies on them.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.