Lesson 2 • Beginner
Variables and Data Types 🐹
Master Go's type system — from short declarations with := to slices, maps, and explicit type conversion.
What You'll Learn in This Lesson
- • Three ways to declare variables (
var, type inference,:=) - • Go's basic types, zero values, and constants with
iota - • Explicit type conversion (no implicit casting)
- • Arrays, slices, and maps
- • Control flow: if, for, switch, and range
1️⃣ Variable Declarations
Go gives you three ways to declare variables. The short declaration := is most common inside functions. Constants use const and the special iota keyword for auto-incrementing enumerations.
Try It: Declarations
Three ways to declare variables and constants
// Variable Declarations in Go
console.log("=== Three Ways to Declare Variables ===");
console.log();
// Method 1: var with type
console.log("// Method 1: Explicit type");
console.log('var name string = "Alice"');
console.log("var age int = 28");
console.log("var height float64 = 5.7");
console.log("var isActive bool = true");
console.log();
// Method 2: var with inference
console.log("// Method 2: Type inference (var)");
console.log('var city = "London" // inferred as string');
console.lo
...2️⃣ Types & Type Conversion
Go is statically typed — every variable has a fixed type. Unlike JavaScript or Python, Go has no implicit type conversion. You must explicitly convert between types, which catches bugs at compile time.
Try It: Types
Basic types, zero values, and explicit type conversion
// Go's Type System
console.log("=== Basic Types ===");
console.log();
const types = [
["bool", "true / false", "false"],
["int", "Platform-dependent integer", "0"],
["int8/16/32/64", "Sized integers", "0"],
["uint", "Unsigned integer", "0"],
["float32/64", "Floating point numbers", "0.0"],
["string", "UTF-8 text (immutable)", '""'],
["byte", "Alias for uint8", "0"],
["rune", "Alias for int32 (Unicode)", "0"],
];
console.log(" Type Description Zero
...3️⃣ Slices & Maps
Slices are Go's dynamic arrays — you'll use them constantly. Maps are key-value pairs (like dictionaries). Both are reference types, built into the language with first-class support.
Try It: Slices & Maps
Dynamic arrays, maps, and common operations
// Arrays, Slices, and Maps
console.log("=== Arrays (Fixed Size) ===");
console.log(" var nums [3]int = [3]int{10, 20, 30}");
console.log(" // Fixed size — rarely used directly");
console.log();
console.log("=== Slices (Dynamic — You'll Use These!) ===");
let fruits = ["Apple", "Banana", "Cherry"];
console.log(" fruits := []string{\"Apple\", \"Banana\", \"Cherry\"}");
console.log(" Length:", fruits.length);
console.log();
// Append
fruits.push("Date");
console.log(" fruits = append(fruits
...4️⃣ Control Flow
Go has only one loop keyword: for. It handles classic loops, while-style loops, infinite loops, and range iteration. Switch statements don't fall through by default — no more forgotten break statements.
Try It: Control Flow
if/else, for loops, range, and switch statements
// Control Flow in Go
console.log("=== if/else ===");
console.log(" // No parentheses around condition!");
console.log(" if age >= 18 {");
console.log(' fmt.Println("Adult")');
console.log(" } else {");
console.log(' fmt.Println("Minor")');
console.log(" }");
console.log();
console.log(" // if with initialization statement:");
console.log(' if err := doSomething(); err != nil {');
console.log(" // handle error");
console.log(" }");
console.log();
console.log("=== for Loop
...⚠️ Common Mistakes
_ to discard: _, err := function().[3]int is a fixed array, []int is a slice. Almost always use slices.:= for local variables and var for package-level or when you need an explicit type.📋 Quick Reference
| Pattern | Go Syntax |
|---|---|
| Short declare | name := "Alice" |
| Slice | nums := []int{1, 2, 3} |
| Append | nums = append(nums, 4) |
| Map | m := map[string]int{} |
| Range | for i, v := range slice {} |
| Convert | f := float64(intVal) |
🎉 Lesson Complete!
You've mastered Go's type system, slices, maps, and control flow. Next, we'll learn functions and methods!
Sign up for free to track which lessons you've completed and get learning reminders.