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
- 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
- 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
Go "panic: test timed out after 10m0s" - Fix in CIFix Go "panic: test timed out after 10m0s" in CI - a test exceeded the default timeout, usually a hang or a s…
Go test "all goroutines are asleep - deadlock" - Fix in CIFix Go "fatal error: all goroutines are asleep - deadlock!" in a test in CI - every goroutine is blocked with…
Go "fatal error: concurrent map read and map write" - Fix in CIFix Go "fatal error: concurrent map read and map write" in CI - a map was accessed from multiple goroutines w…