Loading
0x70Lesson 8 of 9

Aggregate data with maps

Group and count values using map lookups and updates.

14 min 4-question quiz 1 code exercise
By the end of this lesson you can
  • Use Go maps to build per-key totals and frequencies.

A map in Go stores key-value pairs and is ideal for grouped summaries like counts per tag or totals per region. Use the “comma ok” form (value, ok := map[key]) to handle missing keys safely.

main.go
1package main
2
3import "fmt"
4
5func main() {
6    counts := map[string]int{}
7    counts["go"]++
8    counts["go"]++
9    fmt.Println(counts["go"])
10}
Output
2

Map iteration order is not guaranteed. If output format matters, print keys in a fixed order or sort them first. Deterministic output keeps tests and downstream integrations stable.

Key takeaways

  • Use Go maps to build per-key totals and frequencies.

  • Go catches many mistakes while compiling.

  • Use small experiments to understand each new language feature.

Lesson quiz

4 questions · pass with 3 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write Go

Write Go in the editor and run it against sample input with a remote compiler. Your code is sent to Compiler Explorer to compile and run.

Exercise 1

Count Go tags

+25 XP

Read a count and then that many tags. Print how many times go appears.

  • Two go tags
  • No go tag
main.go
Loading editor…

Go is compiled and run by Compiler Explorer (godbolt.org), a free public service. Your source code and test input are sent to that service.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…