What Is a Nil Pointer in Go

A nil pointer in Go is a null reference that causes a runtime error when used with panic in Go 1.21+ unless the panicnil GODEBUG setting is enabled.

A nil pointer in Go is a pointer variable that holds no memory address, representing the absence of a value. In Go 1.21 and later, calling panic(nil) triggers a runtime error by default unless you explicitly restore the old behavior using the panicnil GODEBUG setting. To allow panic(nil) again, set the environment variable or use a directive in your source file:

export GODEBUG=panicnil=1

Alternatively, add this directive to the top of your Go source file:

//go:debug panicnil=1