Module 2: Variables & Types (The Strict Containers)
📚 Module 2: Variables & Types
Course ID: GO-102
Subject: The Strict Containers
In Go, we don’t just “save data.” We put data into specific Containers. Every container has a strict size and a specific purpose.
🏗️ Step 1: The “What is it?” Problem
In Python, you can say x = 5 and later x = "hello". Go does NOT allow this.
🧩 The Analogy: The Cargo Ship
- If you have a container labeled “Fruit”, you cannot put “Bicycles” in it.
- If you try, the Crane Operator (The Compiler) will refuse to lift the container.
🏗️ Step 2: The Three Ways to Create Variables
Go is flexible but strict. There are three common ways to define a variable:
- The Explicit Way:
var name string = "Gopher"(Very clear). - The Short Way:
age := 25(Go guesses the type for you—most popular!). - The Constant Way:
const PI = 3.14(This container can never be opened or changed).
🏗️ Step 3: Zero Values (The “Default Setting”)
In many languages, if you create a variable but don’t give it a value, the app crashes (Null Pointer). In Go, every container comes with a Default Setting.
- int: 0
- string: "" (Empty)
- bool: false
🧩 The Analogy: The Clean Room
When you rent a hotel room in Go, it is never “Messy” (random data). It is always Cleaned to the default setting before you enter.
🥅 Module 2 Review
- Strong Typing: You cannot change the type of a variable after it is created.
- := (Short Declaration): Go’s way of being fast like Python while staying strict.
- Zero Value: Go automatically “cleans” your variables to a safe default.
:::tip Slow Learner Note You’ll see := used 90% of the time. Just remember: it only works inside functions! :::