invalid memory address or nil pointer dereference 🐹 Go

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

  1. Check err before using the result
  2. Initialise structs/maps (&T{}, make(map…))
  3. Guard with if x == nil

Detailed fix by platform

Go

  1. go
    resp, err := http.Get(url)
    if err != nil {
        return fmt.Errorf("fetch: %w", err)
    }
    defer resp.Body.Close()

How to diagnose

  1. Stack — First frame in your package
  2. 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:

🧠 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.