ProgramGuru

Working with Modern Go Collections

Start with Working with Modern Go Collections

Modern Go provides generic helpers for slices, maps and iterators that can make common collection operations clearer.

What you’ll learn

  • Explain the main purpose of working with modern go collections.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

Since Go 1.23, maps.Keys(m) produces an iterator over keys and slices.Sorted collects and sorts iterator values. slices.Values yields slice values without writing a manual indexing loop. Iterator outputs can be consumed directly by range or collected into slices.

Map order is not defined; combining maps.Keys with slices.Sorted produces a reliable presentation order. Avoid assuming that collecting, sorting or copying is free: these operations may allocate storage or do extra work.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for collectionsCompose adapters to control iteration and presentation.Map keys (unordered)Step 1maps.Keys iteratorStep 2slices.SortedStep 3Predictable loop orderStep 4Compose adapters to control iteration and presentation.
Compose adapters to control iteration and presentation.

Tools, syntax and practical notes

Requires Go 1.23 or later for the iterator helpers. References: maps and slices.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import (
	"fmt"
	"maps"
	"slices"
)

func main() {
	points := map[string]int{"Mira": 8, "Ava": 10, "Noah": 7}
	keys := slices.Sorted(maps.Keys(points))
	for _, name := range keys {
		fmt.Println(name, points[name])
	}
}

Output

Ava 10
Mira 8
Noah 7

Trace what the program does

maps.Keys yields names in an unspecified order. slices.Sorted collects them and returns alphabetical order, so the printed lines are Ava, Mira and Noah with their corresponding scores.

Avoid common mistakes

  • Never depend on direct map iteration order for stable presentation.
  • slices.Sorted creates a collected, sorted slice; it does not mutate the original map.

Try it yourself

Use slices.Values on a slice of three scores, then compute their sum with a range-over-function loop.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

Why pair maps.Keys with slices.Sorted?
Choose one answer. You can try again.

Answer and explanation

To produce deterministic key order

Correct. maps.

What you should remember

You have practiced Working with Modern Go Collections in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Introduction to Concurrency →