0
0
GoComparisonBeginner · 3 min read

Nil Map vs Empty Map in Go: Key Differences and Usage

In Go, a nil map is a map variable that has not been initialized and points to nil, while an empty map is initialized but contains no key-value pairs. You can read from both safely, but writing to a nil map causes a runtime panic, whereas writing to an empty map works normally.
⚖️

Quick Comparison

This table summarizes the main differences between a nil map and an empty map in Go.

AspectNil MapEmpty Map
InitializationNot initialized, value is nilInitialized with make or literal, but empty
Memory allocationNo memory allocatedMemory allocated for map structure
Reading keysSafe, returns zero valueSafe, returns zero value
Writing keysCauses runtime panicWorks normally, adds key-value pairs
Length (len())Returns 0Returns 0
Use caseIndicates uninitialized mapRepresents an empty but ready-to-use map
⚖️

Key Differences

A nil map in Go is a map variable that has not been assigned any memory or initialized. Its value is literally nil. You can safely read from a nil map, and it will return the zero value for the value type, but if you try to write to it, the program will panic at runtime because the map has no underlying data structure to hold new entries.

On the other hand, an empty map is a map that has been initialized using make or a map literal but currently holds no key-value pairs. It has allocated memory and can safely handle both reads and writes. Writing to an empty map adds new entries without any errors.

Both nil and empty maps return zero length when checked with len(). However, the key difference is in write operations: nil maps cause a panic on writes, while empty maps do not. This distinction is important when you want to ensure your map is ready for use or to detect if it has been initialized.

⚖️

Code Comparison

This example shows how a nil map behaves when reading and writing keys.

go
package main

import "fmt"

func main() {
	var nilMap map[string]int // nil map

	fmt.Println("Length of nilMap:", len(nilMap))
	fmt.Println("Read from nilMap[\"key\"]:", nilMap["key"])

	// Writing to nil map causes panic
	// Uncommenting the next line will cause runtime panic
	// nilMap["key"] = 1
}
Output
Length of nilMap: 0 Read from nilMap["key"]: 0
↔️

Empty Map Equivalent

This example shows how an empty map behaves when reading and writing keys.

go
package main

import "fmt"

func main() {
	emptyMap := make(map[string]int) // empty map

	fmt.Println("Length of emptyMap:", len(emptyMap))
	fmt.Println("Read from emptyMap[\"key\"]:", emptyMap["key"])

	emptyMap["key"] = 1 // Writing works fine
	fmt.Println("After write, emptyMap[\"key\"]:", emptyMap["key"])
}
Output
Length of emptyMap: 0 Read from emptyMap["key"]: 0 After write, emptyMap["key"]: 1
🎯

When to Use Which

Choose a nil map when you want to represent an uninitialized or optional map and want to detect if it has been set. It saves memory until initialization but requires care to avoid writing before initialization.

Choose an empty map when you want a map ready for immediate use, especially if you plan to add or update entries. It avoids runtime panics and simplifies code by removing the need to check for nil before writing.

Key Takeaways

A nil map is uninitialized and causes panic on writes, but safe for reads.
An empty map is initialized, safe for both reads and writes.
Use nil maps to represent uninitialized state and empty maps for ready-to-use maps.
Always initialize maps before writing to avoid runtime panics.
len() returns 0 for both nil and empty maps.