Variables, types, functions, and control flow. Every concept mapped from Python.
print("Hello, World!")
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Run it: Save as
main.go, run withgo run main.go
Python is dynamically typed. Go is statically typed. This is the biggest shift.
name = "Alice"
age = 30
height = 5.9
is_cool = True
// 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, usevar.
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,0for numbers,falsefor bool.
def add(a, b):
return a + b
def greet(name):
return f"Hello, {name}!"
func add(a int, b int) int {
return a + b
}
func greet(name string) string {
return fmt.Sprintf("Hello, %s!", name)
}
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)
}
if age >= 21:
print("Can drink")
elif age >= 18:
print("Can vote")
else:
print("Too young")
if age >= 21 {
fmt.Println("Can drink")
} else if age >= 18 {
fmt.Println("Can vote")
} else {
fmt.Println("Too young")
}
for i in range(5):
print(i)
for i, item in enumerate(items):
print(i, item)
for i := 0; i < 5; i++ {
fmt.Println(i)
}
for i, item := range items {
fmt.Println(i, item)
}
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]
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!
// 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
}
# 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
okis conventional for the boolean.
Progress through each section in order, or jump to where you need practice.
Practice individual concepts you just learned.
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!
func name(params) returnType { }func f() (int, error)[]Type{} - dynamic arraysmap[KeyType]ValueType{} - like dictionariesvalue, ok := map[key] - check if a map key existsa, b = b, a - swap without temp variablefor i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1slow := 0 outside, for fast := 1; ... insideresult := []Type{}, then result = append(result, item)[]rune(s) for Unicodevar at package levelYou now have the fundamentals to solve many LeetCode Easy problems! Module 2 will cover pointers, structs, and methods - the building blocks for more complex programs.