PY
GO

Python to Go

10 lessons

Progress0%
1Variables & Types2Functions3Lists → Slices, Dicts → Maps4Classes → Structs5asyncio → Goroutines6Modules → Packages7Interfaces8Error Handling Patterns9Testing10Standard Library
All Mirror Courses
PY
GO
Lists → Slices, Dicts → Maps
MirrorLesson 3 of 10
Lesson 3

Lists → Slices, Dicts → Maps

Core collection types

Introduction

In this lesson, you'll learn about lists → slices, dicts → maps in Go. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
PY
From Python:

In Python, you're familiar with core collection types.

GO
In Go:

Go has its own approach to core collection types, which we'll explore step by step.

The Go Way

Let's see how Go handles this concept. Here's a typical example:

GO
Go Example
package main

import "fmt"

// Slice (like Python list)
nums := []int{1, 2, 3}
nums = append(nums, 4)
sub := nums[1:3]

// Map (like Python dict)
ages := map[string]int{"Alice": 30, "Bob": 25}
ages["Charlie"] = 35
val, ok := ages["Alice"] // ok = false if missing
delete(ages, "Bob")

// No comprehensions — use loops
doubled := make([]int, len(nums))
for i, n := range nums {
    doubled[i] = n * 2
}

// Range over map
for k, v := range ages {
    fmt.Println(k, v)
}

Comparing to Python

Here's how you might have written similar code in Python:

PY
Python (What you know)
# List
nums = [1, 2, 3]
nums.append(4)
sub = nums[1:3]

# Dict
ages = {"Alice": 30, "Bob": 25}
ages["Charlie"] = 35
val = ages.get("Alice", 0)
del ages["Bob"]

# Comprehensions
doubled = [n * 2 for n in nums]
evens = {n for n in nums if n % 2 == 0}

# Tuple unpacking
for k, v in ages.items():
    print(k, v)
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

Python list → Go slice; append works in both

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

Python dict → Go map[K]V; access returns (value, ok) in Go

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

Go has no list/dict comprehensions — use loops

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

Go delete(m, k) → Python del d[k]

Step-by-Step Breakdown

1. Slices

Go slices work like Python lists — dynamic, with append and slicing. The syntax is almost identical.

PY
Python
nums.append(4); sub = nums[1:3]
GO
Go
nums = append(nums, 4); sub := nums[1:3]

2. Map Access

Go map access returns a second bool indicating if the key existed. Python's dict.get(key, default) has a different idiom.

PY
Python
val = ages.get("Alice", 0)
GO
Go
val, ok := ages["Alice"]
if !ok { val = 0 }

3. No Comprehensions

Go has no list comprehensions. Write explicit for loops — they're clearer in Go's style.

PY
Python
doubled = [n * 2 for n in nums]
GO
Go
doubled := make([]int, len(nums))
for i, n := range nums { doubled[i] = n * 2 }

Common Mistakes

When coming from Python, developers often make these mistakes:

  • Python list → Go slice; append works in both
  • Python dict → Go map[K]V; access returns (value, ok) in Go
  • Go has no list/dict comprehensions — use loops
Common Pitfall
Don't assume Go works exactly like Python. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Python list → Go slice; append works the same
  • Python dict → Go map[K]V
  • Map access: val, ok := m[key]
  • No comprehensions — use range loops
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in Go to practice these concepts.
PreviousNext