nil pointer dereference
Go Programming Language
Severity: CriticalWhat Does This Error Mean?
A nil pointer dereference in Go means your code tried to access a value through a pointer that is nil (empty). When a pointer is nil, it points to nothing. Trying to read or write through it causes an immediate program crash (panic). This is one of the most common runtime errors in Go. Fix it by checking that a pointer is not nil before using it.
Affected Models
- Go 1.0 and later
- All Go versions
- go build, go run, go test
Common Causes
- Using a pointer variable that was declared but never assigned a real value
- A function returned nil instead of a valid pointer, and the caller did not check for nil before using the result
- Accessing a field or method on a struct pointer that was never initialized
- Iterating over a nil slice or map that was never initialized
- A race condition where one goroutine sets a pointer to nil while another goroutine uses it
How to Fix It
-
Look at the stack trace from the panic. It shows the exact line where the nil dereference happened.
Run your program in development with panic output visible. The line number in the trace points directly to the problem.
-
Find where the pointer or value comes from. Check if the function that creates it can return nil.
Many Go functions return (value, error). If an error occurred, the value is often nil. Always check the error before using the value.
-
Add a nil check before using any pointer: if myPointer != nil { ... }
This is the standard pattern. If nil means an error state, return an error or log a message instead of proceeding.
-
Initialize pointers before use. For structs, use: myStruct := &MyStruct{} instead of declaring var myStruct *MyStruct.
var myStruct *MyStruct gives you a nil pointer. &MyStruct{} gives you a pointer to a new, empty struct.
-
For maps, always initialize with make: myMap := make(map[string]int) before adding or reading keys.
Reading from a nil map returns the zero value (safe), but writing to a nil map causes a panic.
When to Call a Professional
A nil pointer dereference is a runtime panic that crashes your Go program immediately. If this happens in production, it will bring down the service unless recovered. Fix it by adding nil checks wherever a pointer could be nil before it is used.
Frequently Asked Questions
What is a pointer in Go?
A pointer is a variable that stores the memory address of another value. Instead of holding the value directly, it holds a reference to where the value lives in memory. When a pointer is nil, it holds no address — it points to nothing. Trying to use a nil pointer is like following a map that leads nowhere.
How do I find which variable is nil when the program crashes?
Look at the line in the stack trace. Every variable on that line that is a pointer could be nil. Add fmt.Println statements above the crashing line to print the values just before the crash. Or use a debugger like Delve (dlv) to step through the code and inspect values.
Can I recover from a nil pointer dereference without crashing?
Yes, using recover() inside a deferred function: defer func() { if r := recover(); r != nil { ... } }() But this should be a last resort — for example in an HTTP handler that should not crash the whole server. The right fix is always to prevent the nil dereference with a proper nil check.