panic: runtime error: invalid memory address or nil pointer dereference

Go · updated 2026-11-13

A Go program panics when it reads a field or calls a method through a nil pointer. The goroutine stack under the panic names the function and the line; the nil is usually the struct pointer on that line (o.Customer here), most often one that a constructor, a lookup or an unmarshal left unset.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x1021d8b8c]

goroutine 1 [running]:
main.receiptEmail(...)
	/app/main.go:6
main.main()
	/app/main.go:10 +0x1c

Quick fix

Open the first frame under 'goroutine N [running]', look at that line and find which pointer is nil (a struct field, a map value, a returned pointer you didn't check). Check it where it can be nil and return an error instead of using it, and fix the code that should have set it. In an HTTP server, net/http recovers the panic per request, so you see it in the log rather than as a crash.

  1. Read the first frame under the running goroutine: it is the function and line that dereferenced nil.
  2. On that line, list every pointer it follows (o, o.Customer, a map lookup that returns a pointer) and find the nil one.
  3. Where the value can legitimately be missing, check for nil and return an error the caller handles.
  4. Where it must never be nil, fix the constructor or the lookup that left it unset, and test that path.
  5. Check every err before using the value next to it: a nil pointer often follows an ignored error from a lookup or json.Unmarshal.
go
func receiptEmail(o *Order) (string, error) {
	if o == nil || o.Customer == nil {
		return "", fmt.Errorf("order %v has no customer", o)
	}
	return o.Customer.Email, nil
}

// And check the error next to the value:
c, err := store.FindCustomer(ctx, id)
if err != nil {
	return fmt.Errorf("find customer %s: %w", id, err)
}
Fix it with your coding agent

Paste this into Claude Code, Cursor or Codex in the project that throws it. The agent reads this guide in plain text and applies the fix that matches your stack.

prompttext
Read https://runheed.com/errors/go-nil-pointer-dereference.md and fix this error in this project.

Why it happens

A pointer field was never set

A struct built with &Order{} or decoded from JSON without the field leaves pointer fields nil. Code written for the full object reads o.Customer.Email and panics on the first order without a customer.

An error was ignored

Functions return (nil, err) on failure. Dropping err with _ or reading the value before checking err hands a nil pointer to the next line.

A map holds pointers and the key is missing

m[key] returns the zero value, which is nil for a pointer type, so m[key].Field panics. Use the two-value form v, ok := m[key].

A nil interface or method value

Calling a method on a nil interface, or a nil func variable, panics the same way. A typed nil pointer stored in an interface is not == nil, which hides the problem until the method runs.

Other fixes

  • Run staticcheck (check SA5011) or Uber's nilaway in CI: both flag many possible nil dereferences before they ship.
  • Return values instead of pointers for small structs that must always be present.
  • In goroutines you start yourself, recover() and report the panic: net/http only recovers panics in request handlers.

Same error, other wording

  • panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation]
  • http: panic serving 10.0.0.1:52344: runtime error: invalid memory address or nil pointer dereference
  • panic: assignment to entry in nil map

Let your coding agent catch the next one

Runheed records this error in production with the stack trace, the breadcrumbs before it and, when you set it, the agent session that shipped the code, and serves it to Claude Code, Cursor or any MCP client. With it connected, you can ask:

> Find the nil pointer dereference panics in production, show the frame and the request behind the latest one, work out which pointer was nil and why, and add the nil check or the missing initialization.

Setup for Go, then have your agent fix and resolve it.

Related errors

More in Undefined, None and type errors

References: The Go Programming Language Specification: run-time panics, Effective Go: errors and panic/recover, staticcheck SA5011: possible nil pointer dereference

Spotted a mistake? Email hello@greatwork.company. About Runheed.