Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A nil pointer in Go is an uninitialized pointer variable that does not reference any valid memory address. It represents the zero value for any pointer type, conceptually pointing to the unmapped memory address 0x0. The predeclared identifier nil itself is untyped and has no default type. However, a nil pointer is strongly typed because the pointer variable it is assigned to has a specific declared type. Therefore, a nil *int and a nil *string are distinct types to the compiler, even though both evaluate to nil.

Syntax and Declaration

When a pointer is declared without an explicit initialization, the Go compiler automatically assigns it the zero value, which is nil.
package main

func main() {
    var p *int        // p is a nil pointer of type *int
    p2 := (*int)(nil) // Explicitly casting the untyped nil to a specific pointer type
    
    _, _ = p, p2      // Prevent 'declared and not used' compiler errors
}

Technical Characteristics

1. Dereferencing and Panics

Dereferencing a pointer means accessing the value stored at the memory address the pointer holds. Because a nil pointer points to an invalid memory address (the zero page of the process’s virtual memory space), attempting to dereference it results in a segmentation violation at the OS level. The Go runtime intercepts this and triggers a runtime panic.
package main

func main() {
    var p *int
    
    // The following line causes: 
    // panic: runtime error: invalid memory address or nil pointer dereference
    _ = *p 
}

2. Type Strictness

While multiple pointers can be nil, they cannot be compared to one another if their base types differ. The compiler enforces strict type checking.
package main

import "fmt"

func main() {
    var pInt *int
    var pStr *string

    fmt.Println(pInt == nil) // true
    fmt.Println(pStr == nil) // true

    // fmt.Println(pInt == pStr) // COMPILE ERROR: mismatched types *int and *string
}

3. Nil Pointer Method Receivers

Unlike many object-oriented languages where invoking a method on a null reference throws an immediate exception, Go permits method calls on nil pointers. The method is executed normally, and the nil value is passed as the receiver argument. It is the responsibility of the method’s internal logic to handle the nil receiver to avoid dereferencing panics.
package main

import "fmt"

type Node struct {
    Value int
}

// The receiver 'n' can safely be nil
func (n *Node) IsNil() bool {
    return n == nil
}

func main() {
    var ptr *Node // ptr is nil
    
    // This executes successfully without panicking
    isNil := ptr.IsNil() 
    
    fmt.Println(isNil) // Output: true
}

Memory Representation

At the runtime level, a pointer in Go is a machine word (8 bytes on a 64-bit architecture, 4 bytes on a 32-bit architecture). A nil pointer is simply a machine word where all bits are set to 0. When evaluating p == nil, the Go runtime is performing a direct comparison of that machine word against 0.
Master Go with Deep Grasping Methodology!Learn More