Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

[Author: @jahidprog
Date: 2025-04-26
Category: interview-qa/pointers
Tags: [go, pointers, memory-management, performance]

Pointers in Golang

A pointer is a variable that stores the memory address of another variable. Pointers are used to:

  • Avoid copying large amounts of data
  • Allow functions to modify the actual value of variables
  • Improve program performance

We use pointers when we want to work with the actual data instead of working on copies.

Pointer Basics

1. How to Declare a Pointer

Use * before the type to declare a pointer.

package main
import "fmt"

func main() {
    var num int = 42
    var ptr *int = &num  // ptr holds the memory address of num
    fmt.Println("Address:", ptr)  // e.g., 0xc000018030
    fmt.Println("Value:", *ptr)   // 42 (dereferencing)
}

2. Zero Value of a Pointer

Uninitialized pointers have a nil value.

package main
import "fmt"

func main() {
    var ptr *int
    fmt.Println(ptr) // <nil>
}

3. Pointer vs Normal Variable

FeatureNormal VariablePointer
StorageHolds valueHolds memory address
ModificationWorks on copyModifies original data

Frequently Asked Questions

1. What is a pointer in Go?

Answer: A variable that stores the memory address of another variable.

var x int = 10
var ptr *int = &x  // ptr points to x

2. How do you declare a pointer?

Answer: Use *datatype.

var ptr *string

3. What is the zero value of a pointer?

Answer: nil.

4. How is a pointer different from a normal variable?

Answer:

  • Normal variable: Stores value directly
  • Pointer: Stores memory address of another variable

5. Does Go support pointer arithmetic like C?

Answer: No, Go deliberately omits pointer arithmetic for safety.

6. How to pass a pointer to a function?

Answer: Use *type parameter.

package main
import "fmt"

func modify(ptr *int) {
    *ptr = 100
}

func main() {
    x := 50
    modify(&x)
    fmt.Println(x) // 100
}

7. Why use pointers for large structs?

Answer: Avoids expensive data copying.

type BigStruct struct { /* many fields */ }

func process(b *BigStruct) {
    // Operates on original struct
}

8. How to create a pointer to a struct?

Answer: Use & operator.

user := User{Name: "Alice"}
userPtr := &user

9. Can you have a pointer to a pointer?

Answer: Yes (double indirection).

a := 10
p1 := &a
p2 := &p1
fmt.Println(**p2) // 10

10. How to check if a pointer is nil?

Answer: Compare with nil.

if ptr == nil {
    fmt.Println("Pointer is nil")
}

11. How do pointers help with memory efficiency?

Answer: They allow sharing data without duplication.

largeData := make([]byte, 1e6) // 1MB data
processData(&largeData)        // Pass address (8 bytes) instead of copying 1MB

12. What's the difference between * and &?

Answer:

  • & gets the address of a variable
  • * dereferences a pointer to access the value
x := 5
ptr := &x  // ptr holds address of x
val := *ptr // val gets 5

13. When should you avoid pointers?

Answer:

  • With small data types (int, bool etc.) where copying is cheap
  • When immutability is desired
  • In concurrency scenarios where shared access could cause races

14. How do pointers relate to Go's garbage collection?

Answer: The GC tracks pointers to determine object reachability. Unreachable objects (no pointers to them) get collected.

15. Can you use pointers with interfaces?

Answer: Yes, but the rules differ:

var w io.Writer
buf := new(bytes.Buffer)
w = buf  // No explicit pointer needed

Key Takeaways:

  1. Pointers provide direct memory access
  2. Use & to get addresses, * to dereference
  3. Essential for efficient large data handling
  4. Go pointers are safer than C (no arithmetic)