Maps: Key-Value Stores
Creating, reading, writing and deleting map entries
Maps: Key-Value Stores is a free Go Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Map?
A map in Go is an unordered collection of key-value pairs. Keys must be a comparable type (strings, ints, etc.). Maps are reference types backed by a hash table.
Creating Maps
Use a literal or make:
package main
import "fmt"
func main() {
ages := map[string]int{"Alice": 30, "Bob": 25}
cache := make(map[string]string)
fmt.Println(ages, cache)
}Reading and Writing
Access values with m[key] and assign with m[key] = value. Reading a missing key returns the zero value:
package main
import "fmt"
func main() {
m := map[string]int{}
m["hits"] = 10
m["hits"]++
fmt.Println(m["hits"]) // 11
fmt.Println(m["missing"]) // 0
}Checking Key Existence
The two-value form distinguishes a missing key from a zero value:
package main
import "fmt"
func main() {
m := map[string]int{"a": 0}
v, ok := m["a"]
fmt.Println(v, ok) // 0 true
v2, ok2 := m["z"]
fmt.Println(v2, ok2) // 0 false
}Deleting Entries
Use the built-in delete function. Deleting a non-existent key is a no-op:
package main
import "fmt"
func main() {
m := map[string]int{"x": 1, "y": 2}
delete(m, "x")
delete(m, "z") // no-op
fmt.Println(m) // map[y:2]
}Iterating a Map
Use range to iterate. Order is not guaranteed:
package main
import "fmt"
func main() {
scores := map[string]int{"Alice": 90, "Bob": 85}
for name, score := range scores {
fmt.Printf("%s: %d\n", name, score)
}
}Maps with Struct Values
Maps can hold structs as values. Update via a temporary variable (structs in maps are not addressable):
package main
import "fmt"
type Point struct{ X, Y int }
func main() {
points := map[string]Point{"A": {1, 2}}
p := points["A"]
p.X = 99
points["A"] = p
fmt.Println(points["A"]) // {99 2}
}nil Maps
A nil map reads like an empty map (zero values), but writing to a nil map panics:
package main
func main() {
var m map[string]int
_ = m["key"] // ok — returns 0
// m["key"] = 1 // PANIC: assignment to nil map
}Maps Are Not Safe for Concurrent Use
Go's built-in map is not goroutine-safe. Concurrent reads are fine, but concurrent writes (or read+write) require a mutex or sync.Map:
package main
import "sync"
func main() {
var mu sync.Mutex
m := map[string]int{}
mu.Lock()
m["key"] = 1
mu.Unlock()
_ = m
}Counting Frequencies
A classic map pattern — counting occurrences:
package main
import "fmt"
func main() {
words := []string{"go", "is", "fast", "go", "is", "go"}
freq := map[string]int{}
for _, w := range words {
freq[w]++
}
fmt.Println(freq) // map[fast:1 go:3 is:2]
}Quick Check
What happens when you read a key that doesn't exist in a Go map?
Recap: Maps
Key takeaways:
- Maps are unordered key-value stores
- Use the two-value form to check key existence
- Never write to a nil map
- Iteration order is random — sort keys if needed
- Protect maps with a mutex in concurrent code
Practice Prompt
Write a function that takes a string and returns a map[rune]int counting how many times each character appears. Test it with the string "hello".
Frequently asked questions
Is the “Maps: Key-Value Stores” lesson free?
Yes — the full text of “Maps: Key-Value Stores” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Maps: Key-Value Stores”?
Creating, reading, writing and deleting map entries You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Maps: Key-Value Stores” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Arrays: Fixed-Length Collections
- Slices: Dynamic Lists
- Maps: Key-Value Stores
- Iterating Collections with range