02open 25 min

Syntax and data: fewer shortcuts, clearer shapes

Translate functions, control flow, slices, and maps into Go's more explicit syntax.

by the end of this lesson you can

  • Uses a concrete Go type for each record
  • Builds the result with append rather than comprehension syntax
  • Keeps the filtering condition visible in the loop body

Overview

Python syntax is optimized for fluency. Go syntax is optimized for explicitness. The code is usually a little more verbose, but the data structures and control flow are easier to scan quickly.

In Python, you often

write concise loops, flexible function signatures, and literal-heavy data structures that trade ceremony for speed of expression.

In Go, the common pattern is

to be more explicit about variable declaration, iteration, and container types. Slices and maps do most of the everyday work that lists and dicts do in Python.

why this difference matters

This module is where many Python developers first feel friction. Once you accept the extra syntax as a tool for readability, Go code starts to feel much more regular.

Python

items = [name.upper() for name in names]
ages = {"ana": 30, "lee": 28}

Go

var items []string
for _, name := range names {
    items = append(items, strings.ToUpper(name))
}
ages := map[string]int{"ana": 30, "lee": 28}

Deeper comparison

Python version

users = [
    {"name": "ana", "active": True},
    {"name": "lee", "active": False},
]
active_names = [user["name"].upper() for user in users if user["active"]]

Go version

type User struct {
    Name   string
    Active bool
}

var activeNames []string
for _, user := range users {
    if user.Active {
        activeNames = append(activeNames, strings.ToUpper(user.Name))
    }
}

Reflect

What do you gain, and what do you lose, when a Python comprehension becomes an explicit Go loop?

what a strong answer notices

A strong answer mentions readability of control flow, clearer data shapes, and the trade-off of writing more code for the same transformation.

Rewrite

Rewrite this Python snippet into idiomatic Go using a slice and an explicit loop.

Rewrite this Python

records = [
    {"id": 1, "enabled": True},
    {"id": 2, "enabled": False},
]
ids = [record["id"] for record in records if record["enabled"]]

what good looks like

  • Uses a concrete Go type for each record
  • Builds the result with append rather than comprehension syntax
  • Keeps the filtering condition visible in the loop body

Practice

Design a small Go function that receives a slice of users and returns the email addresses of only the verified users in lowercase.

success criteria

  • Choose a struct shape instead of map-like dynamic access
  • Use a result slice and append within a loop
  • Keep the filtering and transformation steps obvious at the call site

Common mistakes

  • Trying to write Python comprehensions in your head while reading Go loops.
  • Using maps where a small struct would make the code clearer and safer.
  • Assuming slices are just lists with different syntax instead of understanding append and shared backing arrays.

takeaways

  • This module is where many Python developers first feel friction. Once you accept the extra syntax as a tool for readability, Go code starts to feel much more regular.
  • A strong answer mentions readability of control flow, clearer data shapes, and the trade-off of writing more code for the same transformation.
  • Choose a struct shape instead of map-like dynamic access