Sunday, February 8, 2026

Pointer

    Profile Pic of Akash AmanAkash Aman

    Updated: February 2026

    Pointers

    The Pointer Type (*T)

    Concept: A variable that doesn't store data itself (like a number or string), but stores the memory address of another variable.

    • Behavior:
      • The Address Operator (&): Used to "find" where a variable lives in memory.
      • The Dereference Operator (*): Used to "look inside" the address and see or change the value stored there.
    • Why: To avoid making expensive copies of data. Instead of handing someone a massive 500-page book (the data), you give them a sticky note with the library shelf location (the pointer).

    The new() Function

    Concept: A built-in function that allocates memory for a type and returns a pointer to it.

    • Behavior: new(T) allocates "zeroed" storage for a new item of type T and returns its address (*T).
    go
    // Option A: Pointer to an existing variable
    x := 42
    ptrA := &x 
    
    // Option B: Creating a pointer directly
    ptrB := new(int) // Allocates memory for an int, sets it to 0
    *ptrB = 100      // Now it holds 100
    
    • Why: Useful when you want to create a pointer to a type without having to declare a named "dummy" variable first.

    Pass-by-Value vs. Pass-by-Pointer

    Concept: By default, Go is "pass-by-value." When you pass a variable to a function, Go makes a complete copy of it.

    • Behavior:
    go
    func updateValue(n int) {
        n = 100 // Only the copy inside this function changes
    }
    
    func updatePointer(n *int) {
        *n = 100 // The original value at this address is changed
    }
    
    • Why:
      • Pass-by-Value: Safer. The function can't accidentally mess up your original data.
      • Pass-by-Pointer: Necessary when a function needs to modify the original variable, or when the data is so large that copying it would slow down the program.

    The "Nil" Pointer

    Concept: A pointer that has been declared but doesn't point to any specific memory address yet.

    • Behavior: The zero-value of any pointer is nil. Trying to dereference (*p) a nil pointer will cause the program to crash (Panic).
    • Why: It acts as a "null" or "empty" state. It tells the program: "I have a spot for an address, but I'm not looking at anything right now." This is often used to signify that something was not found or hasn't been initialized.

    Pointer Receivers in Structs

    Method Receivers ((t *T) Method())

    Concept: Deciding whether a method belongs to the value or the pointer of a struct.

    go
    type Player struct {
        Health int
    }
    
    // Value Receiver: Operates on a copy
    func (p Player) TakeDamage(amount int) {
        p.Health -= amount 
    }
    
    // Pointer Receiver: Operates on the original
    func (p *Player) Heal(amount int) {
        p.Health += amount
    }
    
    • Behavior:

      • Value Receiver: Even if you change Health inside TakeDamage, the original Player remains at full health.
      • Pointer Receiver: The Heal method actually updates the original Player struct in memory.
    • Why: Consistency. If any single method on a struct needs a pointer receiver to modify data, it is a Go convention to use pointer receivers for all methods on that type to avoid confusion.

    Reference Guides

    Pointer Symbols Cheat Sheet

    SymbolNameActionLiteral Translation
    *intTypeDeclares a pointer"This variable holds the address of an int."
    &myVarAddress OfGets the memory address"Tell me where myVar is sitting in memory."
    *myPtrDereferenceAccesses the value"Go to the address inside myPtr and show me what's there."
    new(int)NewAllocates & returns pointer"Give me a fresh address for an int initialized to 0."

    When to Use Pointers?

    Use a Pointer if...Use a Value if...
    You need to modify the original variable.The variable is a small, basic type (int, bool, string).
    The struct is large (saves memory/CPU).The data should be immutable (should not change).
    You need to represent absence (nil).You want to avoid the risk of nil-pointer panics.

    © 2026 Akash Aman | All rights reserved