Chapter 04: Variables & Data
Computers are not magic. They are machines that manipulate data in Memory. To be a professional engineer, you must understand what happens inside the machine.
4.1 The Memory Cell (The Box)
Imagine your computer's RAM (Random Access Memory) as a giant warehouse filled with millions of small numbered boxes.
Each box has:
- An Address: A unique number (e.g.,
0x001). - A Value: What is inside (e.g.,
10).
When you create a Variable, you are claiming one of these boxes and giving it a human name.
var age int = 254.2 Declaration Styles (Why two ways?)
In Go, you will see two ways to create variables.
A. The Formal Way (Explicit)
var username string = "Gopher"Use this when:
- You want to be very clear about the type.
- You want to declare it now but fill it later.
B. The Short Way (Inference)
username := "Gopher":=: Short variable declaration.- It means: "Create the box, look at the value on the right ("Gopher"), guess the type (String), and put it in."
- Constraint: You can ONLY use this inside functions.
TIP
🛡️ Interview Defense: "The Legend"
Interviewer: "When do you use := vs var?"
You: "I prioritize readability and scope control:
- I use
:=(short declaration) for local variables where the type is obvious or inferred, to keep code concise. - I use
varwhen I need to declare a variable without initializing it immediately (zero value), or when specifically initializing package-level variables where:=is not allowed."
4.3 The Zero Value
Go initializes declared variables to a zero value. Some types have nil as their zero value, so initialization does not make every operation safe: dereferencing a nil pointer or writing to a nil map fails. Java local variables require definite assignment; Python names must be bound before use. Neither is accurately described as automatically becoming null or None.
In Go, declared variables have a value. If you don't provide one, Go gives it a "Zero Value".
| Type | Zero Value | Meaning |
|---|---|---|
int | 0 | Empty Count |
float64 | 0.0 | Approximate zero |
bool | false | Default Off |
string | "" | Blank Paper |
pointer | nil | No Address |
var count int
fmt.Println(count) // Output: 0The box is not missing. The box is just empty (contains 0).
4.4 Basic Types (The Shop Objects)
Let's map Go types to real-world objects in our Gopher Shop.
| Go Type | Shop Concept | Example |
|---|---|---|
int | Inventory Count | var stock int = 50 |
int64 | Price in minor units | var priceMinor int64 = 1999 (USD cents) |
float64 | Approximate measurement | var weight float64 = 1.25 |
string | Book Title | var title string = "Go Guide" |
bool | Is On Sale? | var onSale bool = true |
4.5 Constants (The Stone Tablets)
Variables can change (age = 25, then age = 26). Constants cannot.
const Pi = 3.14159Metaphor: Carved in stone. You cannot overwrite it.
4.6 Basic Math (The Calculator)
Computer = Compute.
+(Add):10 + 5->15-(Sub):10 - 5->5*(Mul):10 * 5->50/(Div):10 / 2->5- Note:
10 / 3(Integers) ->3(Cuts off decimal).
- Note:
%(Mod):10 % 3->1(The Remainder).
Visual Anchor:
A Variable is a Labeled Box at a specific Address in the memory warehouse.
🎓 Knowledge Check
- What is the difference between
varand:=?varcan be used anywhere.:=(short declaration) can ONLY be used inside functions. - What is the zero value of an
int?0. - Can you change the type of a variable? No. Go is statically typed. An
intis always anint.
