Operators

Course 1 · Ch 3
Operators, Arithmetic Type Rules, and Control Flow
Doing maths the strict way, comparing values, and branching with if/else and switch — no ternary operator here

Chapter 2 introduced int and float64 as separate types. This chapter shows why that separation matters the moment arithmetic is involved, then covers the operators and branching structures used to make decisions — Go's version of Fundamentals JavaScript Chapter 3, with one notable omission: there is no ternary operator.

Arithmetic Operators

a := 10 b := 3 fmt.Println(a + b) // 13 fmt.Println(a - b) // 7 fmt.Println(a * b) // 30 fmt.Println(a / b) // 3 — integer division truncates, it does NOT round fmt.Println(a % b) // 1 — remainder

+ - * / % work as expected, but / between two int values performs integer division — the decimal part is discarded entirely, not rounded. 10 / 3 gives exactly 3, never 3.33, because both operands are whole numbers.

Go will not silently mix int and float64
var price float64 = 10 / 3 still produces 3 stored as a float — the division itself happened entirely in int first, because both 10 and 3 were untyped integer literals. To get a real decimal result, at least one operand must actually be a float64: 10.0 / 3 gives 3.3333.... Mixing an actual int variable with a float64 variable directly (intVar / floatVar) is a compile error — there is no automatic conversion, unlike JavaScript's number type.

Converting Between Types Explicitly

var count int = 7 var total float64 = 2 result := float64(count) / total // 3.5 — count is converted to float64 first

float64(count) explicitly converts an int to a float64 for that one expression. This explicitness — having to ask for a conversion rather than it happening automatically — is deliberate: it forces a decision about precision instead of letting a type mismatch hide silently.

Comparison and Logical Operators

age := 20 fmt.Println(age == 20) // true fmt.Println(age != 18) // true fmt.Println(age >= 18 && age < 65) // true — && is "and" fmt.Println(age < 13 || age > 100) // false — || is "or"

== != > < >= <= and && || behave identically to JavaScript. One important difference: Go has no === — there's only one equality operator, ==, since Go's static typing already guarantees both sides are the same type, removing the type-coercion ambiguity that == has in JavaScript.

if / else — No Parentheses, Braces Required

age := 16 if age >= 18 { fmt.Println("Adult") } else if age >= 13 { fmt.Println("Teenager") } else { fmt.Println("Child") }

The condition itself has no surrounding parentheses — if age >= 18, not if (age >= 18) — but the curly braces are mandatory, even for a single statement. Omitting them is a compile error, unlike JavaScript where braces are optional for one-line bodies.

switch — Cleaner Than a Chain of else if

day := 3 switch day { case 1: fmt.Println("Monday") case 2, 3, 4, 5: fmt.Println("Midweek") default: fmt.Println("Weekend") } // Midweek

Go's switch needs no break at the end of each case — unlike JavaScript, it never "falls through" to the next case automatically. A single case can also list several values separated by commas (case 2, 3, 4, 5:), avoiding repeated cases entirely.

No ternary operator in Go
JavaScript's condition ? a : b has no Go equivalent at all — Go's designers left it out deliberately, favouring a full if/else even for simple cases, on the principle that it's more readable even though it's more verbose.
JavaScriptGoNotes
if (x) { }if x { }No parentheses around condition; braces always required
x === yx == yOnly one equality operator — types are already guaranteed to match
switch with breakswitch, no break neededGo never falls through by default
cond ? a : bNo equivalentMust use a full if/else
5 / 2 === 2.55 / 2 == 2 (int)Integer division truncates; convert to float64 for a decimal result

Coding Challenges

Challenge 1

Write a program with two int variables, total := 17 and count := 5. Print total / count (integer division), then print the same calculation converted properly to a float64 result using float64(), so it shows the real decimal value.

📄 View solution
Challenge 2

Write a program with score := 72. Use if/else if/else to print "Pass with distinction" (score >= 85), "Pass" (score >= 50), or "Fail" (anything else).

📄 View solution
Challenge 3

Write a program with grade := "B". Use a switch statement to print a description for "A" ("Excellent"), "B" or "C" together ("Satisfactory"), and a default case ("Needs improvement") for anything else.

📄 View solution

Chapter 3 Quick Reference

  • + - * / % — standard arithmetic; int / int truncates instead of rounding
  • float64(x) — explicit conversion; Go never converts numeric types automatically
  • == != > < >= <= && || — same meaning as JavaScript; only one equality operator (==)
  • if x { } else if y { } else { } — no parentheses around the condition, braces always mandatory
  • switch x { case a: ... } — no break needed, no automatic fall-through
  • case a, b, c: — multiple values in one case, comma-separated
  • No ternary operator — always use a full if/else instead
  • Next chapter: loops — for is Go's only loop keyword, covering every JavaScript loop style