go
runtime_error
ai_generated
true
panic: assignment to entry in nil map
ID: go/assignment-to-entry-in-nil-map
95%Fix Rate
95%Confidence
50Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1 | active | — | — | — |
Root Cause
Writing to an uninitialized map. Maps must be created with make() before use.
genericWorkarounds
-
98% success Initialize with make(): m := make(map[string]int)
m := make(map[string]int)
Sources: https://go.dev/blog/maps
-
95% success Or use map literal: m := map[string]int{}
m := map[string]int{}Sources: https://go.dev/blog/maps
-
90% success For struct fields, initialize in constructor or init function
func NewFoo() *Foo { return &Foo{data: make(map[string]int)} }Sources: https://go.dev/blog/maps
Dead Ends
Common approaches that don't work:
-
Check for nil before every map write
70% fail
Verbose and error-prone — just initialize the map
-
Use sync.Map for all maps
65% fail
sync.Map is for concurrent access — overkill for single-goroutine use
Error Chain
Frequently confused with: