Nil Map vs Empty Map in Go: Key Differences and Usage
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.
| Aspect | Nil Map | Empty Map |
|---|---|---|
| Initialization | Not initialized, value is nil | Initialized with make or literal, but empty |
| Memory allocation | No memory allocated | Memory allocated for map structure |
| Reading keys | Safe, returns zero value | Safe, returns zero value |
| Writing keys | Causes runtime panic | Works normally, adds key-value pairs |
Length (len()) | Returns 0 | Returns 0 |
| Use case | Indicates uninitialized map | Represents 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.
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 }
Empty Map Equivalent
This example shows how an empty map behaves when reading and writing keys.
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"]) }
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.