// MODULE 01

Go Fundamentals

Variables, types, functions, and control flow. Every concept mapped from Python.

Your First Go Program

Python
print("Hello, World!")
Go
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

Run it: Save as main.go, run with go run main.go

Variables & Types

Python is dynamically typed. Go is statically typed. This is the biggest shift.

Python
name = "Alice"
age = 30
height = 5.9
is_cool = True
Go
// Explicit types
var name string = "Alice"
var age int = 30

// Type inference with :=
name := "Alice"
age := 30
height := 5.9
isCool := true

Gotcha: := only works inside functions. At package level, use var.

Common Types

Types
string   // "hello"
int      // 42 (platform-dependent size)
int64    // 64-bit integer
float64  // 3.14
bool     // true, false
byte     // alias for uint8
rune     // alias for int32 (Unicode code point)

Zero Values: Uninitialized variables get zero values: "" for string, 0 for numbers, false for bool.

Functions

Python
def add(a, b):
    return a + b

def greet(name):
    return f"Hello, {name}!"
Go
func add(a int, b int) int {
    return a + b
}

func greet(name string) string {
    return fmt.Sprintf("Hello, %s!", name)
}

Multiple Return Values

Multiple returns
func divide(a, b float64) (float64, error) {
    if b == 0 {
        return 0, errors.New("division by zero")
    }
    return a / b, nil
}

result, err := divide(10, 2)
if err != nil {
    fmt.Println(err)
}

Control Flow

If Statements

Python
if age >= 21:
    print("Can drink")
elif age >= 18:
    print("Can vote")
else:
    print("Too young")
Go
if age >= 21 {
    fmt.Println("Can drink")
} else if age >= 18 {
    fmt.Println("Can vote")
} else {
    fmt.Println("Too young")
}

For Loops (The Only Loop)

Python
for i in range(5):
    print(i)

for i, item in enumerate(items):
    print(i, item)
Go
for i := 0; i < 5; i++ {
    fmt.Println(i)
}

for i, item := range items {
    fmt.Println(i, item)
}

Slices & Maps

Slices (Dynamic Arrays)

Slices
nums := []int{1, 2, 3}
nums = append(nums, 4)
fmt.Println(nums[0])     // 1
fmt.Println(len(nums))  // 4
fmt.Println(nums[1:3])   // [2 3]

Maps (Dictionaries)

Creating and using maps
ages := map[string]int{
    "alice": 30,
    "bob":   25,
}
ages["charlie"] = 35  // Add new key

fmt.Println(ages["alice"])  // 30

The Missing Key Problem

In Python, accessing a missing key raises KeyError. In Go, it returns the zero value (0 for int, "" for string, etc). This means you can't tell if a key is missing or just has a zero value!

The "comma ok" idiom - checking if a key exists
// Wrong: Can't tell if dave is missing or age is 0
age := ages["dave"]  // Returns 0 (zero value)

// Right: Use the "comma ok" pattern
// ORDER: value, exists := map[key]
//        ↑      ↑
//        │      └── boolean: true if found, false if not
//        └── the actual value (or zero value)

age, ok := ages["dave"]
// age = 0 (zero value for int)
// ok = false (key doesn't exist)

if ok {
    fmt.Println("Found:", age)
} else {
    fmt.Println("Not found")  // This prints
}

// Common shorthand: declare inside if statement
if age, ok := ages["alice"]; ok {
    //   ↑    ↑
    //   30  true
    fmt.Println("Alice is", age)  // Prints: Alice is 30
}
Python equivalent
# In Python, you might do:
age = ages.get("dave")  # Returns None if missing
if age is not None:
    print(f"Found: {age}")

# Or check with 'in':
if "dave" in ages:
    print(f"Found: {ages['dave']}")

Remember the order!:

First variable = the value you're looking for

Second variable = boolean (did we find it?)

Pattern: value, found := map[key]

You can name them anything, but ok is conventional for the boolean.

Exercises

Progress through each section in order, or jump to where you need practice.

Practice individual concepts you just learned.

💪 Challenges

Combine concepts and learn algorithmic patterns. Each challenge has multiple variants at different difficulties - shuffle to keep things fresh!

Learning Path: Warmups → master basic syntax with quick drills (6 concepts, ~79 variants)

Challenges → 14 exercises with 180 variants across easy/medium/hard difficulties

Progressive difficulty:

• Use the "Get Easier Version" button if a challenge feels too hard

• Use the "Get Harder Version" button to progressively challenge yourself

• Filter by pattern/concept to focus on specific algorithmic techniques

• Shuffle to practice with fresh variants!

Stuck? Check the tactical hints - they show you the code step by step!

Module 1 Summary

Core Concepts

Key Patterns You Learned

Algorithm Patterns & When to Use Them