Skip to content

Troubleshooting guide

Go panic: invalid memory address or nil pointer dereference

Code accessed a field or method through a nil pointer, and Go panicked. The stack trace gives the exact line; the real bug is often the check missed earlier.

3 min read10 sectionsUpdated

On this page

Key points

  • The first goroutine stack shows the line.
  • Ignored errors are the most common source.
  • A typed nil in an interface is not nil.
  • One panic in a goroutine kills the whole program.

What it means

Accessing memory through a nil pointer triggers a runtime panic. Unless recovered, the process prints a stack and exits with status 2. net/http recovers panics inside handlers, so a server may keep running while requests silently fail.

Common causes

  • Ignoring an error and then using the nil result: resp, _ := http.Get(url).
  • Struct fields never initialised, such as a nil dependency.
  • Writing to a nil map.
  • Accessing the result of a lookup that returned no entry.
  • Typed nil values in an interface.

Diagnose it

Read the top of the stack that belongs to your code. The race detector helps when the panic is intermittent.

bash
# Reproduce under the race detector
go test -race ./...
go run -race ./cmd/server

# Show all goroutines in a panic
GOTRACEBACK=all ./server

# Static checks for nil problems
go vet ./...
staticcheck ./...

Fixes

Always handle errors before using a value. Construct dependencies in a constructor that returns an error if something is missing. Initialise maps with make. Check optional pointers explicitly. Add a recovery wrapper to every goroutine you start, as HTTP's recovery covers only handler goroutines.

The typed nil trap

An interface value holds both a type and a value. If a function returns a nil *MyError as an error, then the interface has a type and a nil value, and err != nil is true. Callers then treat success as failure, or call a method that panics. Fix it by returning a literal nil from the function that returns an interface, and by never declaring a concrete-typed error variable to return later. This surprises even experienced Go developers, and code review tends to miss it, since the code reads as correct.

Catch it automatically

Log recovered panics with their stack in log management, and group them through error tracking. A restart count in containers is the other clear signal. See Go performance monitoring.

Frequently asked questions

Can I recover from a nil pointer panic?

Yes, with recover in a deferred function, in the same goroutine. Use it at boundaries, then log and continue.

Does reading from a nil map panic?

No. Reading returns the zero value, whereas writing panics.

Why did the server not crash?

net/http recovers panics inside handlers.

  • ERR_ADDRESS_UNREACHABLE: how to fix it: Fix ERR_ADDRESS_UNREACHABLE: no route to the server, wrong IP in DNS, IPv6 problems, private addresses and firewalls, for visitors and server owners.
  • OpenTelemetry for Go: Instrument a Go service with OpenTelemetry: set up the tracer provider, wrap net/http with otelhttp, pass context everywhere and export the spans.
  • How to monitor Heroku: How to monitor apps on Heroku: H12 timeouts and other router errors, dyno memory (R14), response time, Postgres and Redis add-ons, uptime checks and alerts.
  • Cloudflare error 526: invalid SSL certificate: Fix Cloudflare error 526 Invalid SSL certificate in Full (strict) mode: expired origin certificates, self-signed certs, name mismatches and missing chains.
  • EADDRINUSE: address already in use and how to fix it: Fix EADDRINUSE in Node.js and other servers: find which process holds the port, handle old processes after a crash and avoid race conditions in restarts.
  • Redis monitoring: metrics that matter: Monitor Redis with INFO: memory and evictions, hit rate, clients, latency, persistence and replication, with thresholds and the alerts to set.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.