Skip to content

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:

  1. An Address: A unique number (e.g., 0x001).
  2. 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.

go
var age int = 25

4.2 Declaration Styles (Why two ways?) ​

In Go, you will see two ways to create variables.

A. The Formal Way (Explicit) ​

go
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) ​

go
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 var when 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".

TypeZero ValueMeaning
int0Empty Count
float640.0Approximate zero
boolfalseDefault Off
string""Blank Paper
pointernilNo Address
go
var count int
fmt.Println(count) // Output: 0

The 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 TypeShop ConceptExample
intInventory Countvar stock int = 50
int64Price in minor unitsvar priceMinor int64 = 1999 (USD cents)
float64Approximate measurementvar weight float64 = 1.25
stringBook Titlevar title string = "Go Guide"
boolIs On Sale?var onSale bool = true

4.5 Constants (The Stone Tablets) ​

Variables can change (age = 25, then age = 26). Constants cannot.

go
const Pi = 3.14159

Metaphor: 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).
  • % (Mod): 10 % 3 -> 1 (The Remainder).

Visual Anchor:

A Variable is a Labeled Box at a specific Address in the memory warehouse.

🎓 Knowledge Check
  1. What is the difference between var and :=?var can be used anywhere. := (short declaration) can ONLY be used inside functions.
  2. What is the zero value of an int?0.
  3. Can you change the type of a variable? No. Go is statically typed. An int is always an int.

Released under the MIT License.