02open 25 min

Syntax and data structures: from arrays and objects to slices and maps

Translate array and object habits into Go's slices, maps, and explicit loops.

by the end of this lesson you can

  • Uses a concrete struct shape when appropriate
  • Builds the result with append
  • Keeps the filtering logic visible instead of hiding it in a callback chain

Overview

JavaScript developers often live inside arrays, objects, and method chains. Go still gives you good data structures, but it prefers more visible loops and more explicit data shapes.

In JavaScript/Node, you often

use arrays, objects, and chained methods to keep transformations compact and expressive.

In Go, the common pattern is

to represent collections with slices and maps and keep transformation steps visible with loops and append calls.

why this difference matters

This is where the day-to-day feel of Go becomes real. The code is often more verbose, but the data flow becomes easier to inspect quickly.

JavaScript/Node

const names = users
  .filter((user) => user.active)
  .map((user) => user.name.toUpperCase());

Go

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

Deeper comparison

JavaScript/Node version

const ages = { ana: 30, lee: 28 };
for (const name of ["ana", "lee"]) {
  console.log(ages[name]);
}

Go version

ages := map[string]int{"ana": 30, "lee": 28}
for _, name := range []string{"ana", "lee"} {
    fmt.Println(ages[name])
}

Reflect

When does a Go loop become clearer than a direct translation of a JavaScript method chain?

what a strong answer notices

A strong answer mentions that Go prefers visible control flow when the transformation stops being obvious at a glance.

Rewrite

Rewrite this JavaScript array pipeline into Go using slices and an explicit loop.

Rewrite this JavaScript/Node

const ids = records.filter((r) => r.enabled).map((r) => r.id);

what good looks like

  • Uses a concrete struct shape when appropriate
  • Builds the result with append
  • Keeps the filtering logic visible instead of hiding it in a callback chain

Practice

Design a Go function that groups users by role, starting from the mental model of JavaScript arrays and objects.

success criteria

  • Uses a map intentionally
  • Keeps the grouping logic explicit
  • Avoids over-translating JavaScript object habits where a clearer Go structure exists

Common mistakes

  • Trying to reproduce every JavaScript method chain in Go.
  • Using maps where a struct would make the code easier to read.
  • Thinking of slices as arrays with methods missing rather than as a distinct Go collection tool.

takeaways

  • This is where the day-to-day feel of Go becomes real. The code is often more verbose, but the data flow becomes easier to inspect quickly.
  • A strong answer mentions that Go prefers visible control flow when the transformation stops being obvious at a glance.
  • Uses a map intentionally