Map Internals
How maps store data.
Map Internals is a free Go Academy lesson on CoddyKit — lesson 1 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 stores key-value pairs. Each key maps to one value, and lookups are very fast on average.
In Go you write map[KeyType]ValueType.
package main
import "fmt"
func main() {
ages := map[string]int{"Ann": 30, "Bob": 25}
fmt.Println(ages["Ann"])
}Hash Tables Underneath
Go maps are implemented as hash tables. A hash function turns each key into a number that points to a bucket where the value lives.
Buckets
The map stores entries in buckets. Each bucket holds several key-value pairs. The hash of a key decides which bucket it goes to, giving fast O(1) average lookups.
Creating with make
You can create an empty map with make. Optionally give a size hint to pre-allocate space.
package main
import "fmt"
func main() {
scores := make(map[string]int)
scores["x"] = 10
fmt.Println(scores)
}Nil Maps
A map declared without initialization is nil. You can read from a nil map (you get zero values) but writing to it panics. Always initialize before writing.
package main
import "fmt"
func main() {
var m map[string]int
fmt.Println(m == nil)
fmt.Println(m["missing"])
}Adding and Updating
Assigning to a key adds it if new, or overwrites the existing value.
package main
import "fmt"
func main() {
m := map[string]int{"a": 1}
m["b"] = 2
m["a"] = 99
fmt.Println(m)
}Deleting Keys
The built-in delete removes a key. Deleting a missing key is a safe no-op.
package main
import "fmt"
func main() {
m := map[string]int{"a": 1, "b": 2}
delete(m, "a")
fmt.Println(m)
}Length of a Map
len returns the number of key-value pairs currently in the map.
package main
import "fmt"
func main() {
m := map[string]int{"a": 1, "b": 2, "c": 3}
fmt.Println(len(m))
}Maps Grow Automatically
As you add entries, Go grows the underlying table and may rehash keys into new buckets. This happens automatically, so you do not manage capacity manually.
Allowed Key Types
Keys must be comparable: types you can use with ==. Strings, numbers, booleans, and structs of comparable fields work. Slices and maps cannot be keys.
package main
import "fmt"
func main() {
type Point struct{ X, Y int }
m := map[Point]string{{1, 2}: "origin-ish"}
fmt.Println(m[Point{1, 2}])
}Reference Semantics
A map value is a reference to the underlying data. Passing a map to a function lets that function modify the same map, no pointer needed.
package main
import "fmt"
func addOne(m map[string]int) {
m["count"]++
}
func main() {
m := map[string]int{"count": 0}
addOne(m)
fmt.Println(m)
}Quick Check
What happens if you try to write a key to a nil map?
Recap
Map internals:
- Maps are hash tables with buckets
- Average O(1) lookup, insert, delete
- Keys must be comparable; nil maps panic on write
- Maps have reference semantics
Frequently asked questions
Is the “Map Internals” lesson free?
Yes — the full text of “Map Internals” 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 “Map Internals”?
How maps store data. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Map Internals” 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
- Map Internals
- Checking Existence
- Maps as Sets
- Iteration and Ordering