Dereferencing a pointer variable that points to nil (unallocated memory).
In Go, pointer types hold the memory address of a value. If a pointer variable is `nil` and you try to read, write, or call a method on it via `*ptr` or `ptr.Field`, Go panics with a nil pointer dereference.
Common causes: calling methods on uninitialized struct pointers, attempting to read fields from a failed constructor/function that returned `nil, err` without checking `err != nil`, or accessing nil maps/channels.
1package main2 3import "fmt"4 5type User struct {6 Name string7}8 9func main() {10 var u *User // u is nil11 fmt.Println(u.Name) // Panic: nil pointer dereference!12}1package main2 3import "fmt"4 5type User struct {6 Name string7}8 9func main() {10 // Fix 1: Initialize the struct pointer11 u := &User{Name: "Alice"}12 fmt.Println(u.Name) // "Alice"13 14 // Fix 2: Check for nil before dereferencing15 var optionalUser *User16 if optionalUser != nil {17 fmt.Println(optionalUser.Name)18 } else {19 fmt.Println("User is not instantiated")20 }21}Simulate standard system builds to trigger compiler trace records and track memory crashes locally.
The variable `u` is declared as `*User` but never instantiated with `&User{}` or `new(User)`. Accessing `u.Name` tries to look up memory at 0x0 (nil), triggering a runtime panic.