Go: panic: runtime error: invalid memory address or nil pointer dereference
The program used a pointer, map, interface or struct field that was nil — usually an uninitialised value or an ignored error.
Seen on:
Go
Meaning
Typical cases: using a result after ignoring err (resp is nil when err != nil), calling a method on a nil struct pointer, or writing to a nil map (separate panic). The stack trace’s first line in your code shows where.
Common causes
- Ignored error leaving the result nil (resp, err := http.Get…)
- Struct pointer field never initialised
- Function returning (nil, nil) unexpectedly
- Nil interface/receiver method call
⚡ Quick fix
- Check err before using the result
- Initialise structs/maps (&T{}, make(map…))
- Guard with if x == nil
Detailed fix by platform
Go
- go
resp, err := http.Get(url) if err != nil { return fmt.Errorf("fetch: %w", err) } defer resp.Body.Close()
How to diagnose
- Stack — First frame in your package
- Errors — Any ignored err?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
Other ways to find it
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 7 Oct 2026