Operators
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
+ - * / % 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.
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
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
== != > < >= <= 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
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
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.
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.
| JavaScript | Go | Notes |
|---|---|---|
| if (x) { } | if x { } | No parentheses around condition; braces always required |
| x === y | x == y | Only one equality operator — types are already guaranteed to match |
| switch with break | switch, no break needed | Go never falls through by default |
| cond ? a : b | No equivalent | Must use a full if/else |
| 5 / 2 === 2.5 | 5 / 2 == 2 (int) | Integer division truncates; convert to float64 for a decimal result |
Coding Challenges
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 solutionWrite 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 solutionWrite 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 solutionChapter 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