Go Pointers


Go, unlike Java or Python, offers explicit pointer functionality. Pointers allow you to directly access and manipulate the memory address of a variable. This can be powerful for performance optimization and working with complex data structures, but also requires careful handling to avoid common pitfalls.

In Go, a pointer is represented by a * preceding the type of the variable it points to. The & operator obtains the memory address of a variable.

package main

import "fmt"

func main() {
    x := 10 // Declare an integer variable

    var ptr *int  // Declare a pointer to an integer (currently nil)
    ptr = &x     // Assign the address of x to the pointer ptr

    fmt.Println("Value of x:", x)          // Output: 10
    fmt.Println("Address of x:", &x)       // Output: Memory address (e.g., 0xc000014098)
    fmt.Println("Value of ptr:", ptr)       // Output: Memory address (same as above)
    fmt.Println("Value pointed to by ptr:", *ptr) // Output: 10 (dereferencing)

    *ptr = 20 // Modify the value at the memory address pointed to by ptr
    fmt.Println("New value of x:", x)    // Output: 20 (x is changed!)
}

Key Concepts:

Example: Modifying a Struct through a Pointer:

package main

import "fmt"

type Person struct {
    Name string
    Age  int
}

func updateAge(p *Person, newAge int) {
    p.Age = newAge
}

func main() {
    person := Person{Name: "Alice", Age: 30}
    fmt.Println("Before:", person) // Output: {Alice 30}

    updateAge(&person, 31) // Pass the address of person
    fmt.Println("After:", person)  // Output: {Alice 31}
}

In this example, updateAge receives a pointer to a Person struct. Modifying the struct through the pointer directly affects the original person variable. If updateAge received a copy of the Person struct (without pointers), the changes would be local to the function and wouldn’t affect the original.

Caution:

Pointers are powerful, but they require careful management. Incorrect usage can lead to memory leaks, dangling pointers, or segmentation faults. Always ensure you’re pointing to valid memory locations and handle nil pointer scenarios gracefully.