Skip to main content
Module 4 · Lesson 3 of 518/50 in the course~12 min
Module lessons (3/5)

Map: keys and values

Lesson preview5 hands-on exercises readyCourse Go · Module 4 — Collections

Declaration, access with comma-ok (v, ok := m[k]), delete, non-deterministic iteration.

What you'll learn in this lesson

  • Allocate hash maps with make or literal avoiding write panic to nil map.
  • Distinguish a missing key from a zero value using the comma-ok (v, ok := m[k]) idiom.
  • Apply delete, understand constraints on key types comparable, and write counters and groupings idiomatically.

Overview

The map[K]V type implements a medium access hash table O(1). Like slices, it is a reference type: copying its value shares the same underlying buckets.

If m["count"] returns 0, does this mean that the counter is zero or that the key does not exist? The only certain answer comes from the double value (comma-ok):

Sections covered in this lesson

  1. 1Creating and avoiding nil map panics
  2. 2Comma-ok, delete and zero-value counters
  3. 3Random order and counter pattern
  4. 4Idiomatic set with map[T]struct{}
  5. 5Guided practice
  6. 6Check for understanding
Pro Lesson · Module 4 — Collections

Unlock "Map: keys and values" and the full Course Go curriculum

Declaration, access with comma-ok (`v, ok := m[k]`), `delete`, non-deterministic iteration.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Course Go plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.