Courses/Go/Variables and Data Types

    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

    Try it Yourself »
    JavaScript
    // 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

    Try it Yourself »
    JavaScript
    // 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

    Try it Yourself »
    JavaScript
    // 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

    Try it Yourself »
    JavaScript
    // 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

    ⚠️
    Unused variables — Go won't compile with unused variables. Use _ to discard: _, err := function().
    ⚠️
    Confusing arrays and slices[3]int is a fixed array, []int is a slice. Almost always use slices.
    💡
    Pro Tip: Use := for local variables and var for package-level or when you need an explicit type.

    📋 Quick Reference

    PatternGo Syntax
    Short declarename := "Alice"
    Slicenums := []int{1, 2, 3}
    Appendnums = append(nums, 4)
    Mapm := map[string]int{}
    Rangefor i, v := range slice {}
    Convertf := 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.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service