Skip to content
Latchkey

Go test "panic: nil pointer dereference" - Fix in CI

Dereferencing a nil pointer, calling a method on a nil receiver, or indexing a nil map/slice aborts the test with a runtime panic and a [signal SIGSEGV] stack pointing at the offending line.

What this error means

A test fails with panic: runtime error: invalid memory address or nil pointer dereference and [signal SIGSEGV]. The stack points at a nil receiver, an unchecked error path that left a value nil, or a missing setup step.

go
--- FAIL: TestLookup (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0]
	example.com/app.(*Store).Get(...)

Common causes

Unchecked nil before dereference

A pointer, map, or interface was nil - often from an error path that was not checked - and the code used it anyway.

Missing test setup

A dependency the code expects was never initialized in the test, leaving a nil receiver or field.

How to fix it

Guard the nil and check errors

  1. Check the error before using the value, and guard against nil where it can occur.
Go
s, err := NewStore()
if err != nil { t.Fatal(err) }
if s == nil { t.Fatal("store is nil") }

Initialize dependencies in setup

  1. Construct the value the test needs before exercising it, so no field is nil.
Terminal
go test -run TestLookup ./...

How to prevent it

  • Always check errors before using returned values.
  • Initialize all dependencies in test setup.
  • Add nil guards at boundaries that can legitimately be nil.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →