Skip to Content
Go Realm v1 is released 🎉
TopicsPointer

Pointers in Go

  • A pointer stores the memory address of a variable.
  • & → Gets the address of a variable.
  • * → Dereferences a pointer (accesses the value).
var x int = 10 ptr := &x // ptr holds the address of x fmt.Println(*ptr) // 10 (dereferenced value)

Why Use Pointers?

✔ Modify original values inside functions
✔ Avoid copying large structs (better performance)
✔ Share data across functions

Common Pitfalls

Nil pointer dereference → Runtime panic

var ptr *int fmt.Println(*ptr) // panic: nil pointer dereference

Modifying copies instead of originals

func update(x int) { x = 20 } // Doesn’t modify original func updatePtr(x *int) { *x = 20 } // Works

FAQ

1. What is a pointer in Go?

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

var x int = 10 ptr := &x // ptr holds the address of x

2. Why use pointers?

✔ Modify original values inside functions
✔ Avoid copying large structs (better performance)
✔ Share data across functions

3. What’s the zero value of a pointer?

A: nil (uninitialized pointers point to nothing).

4. How to check for nil pointers?

if ptr != nil { fmt.Println(*ptr) // Safe dereference }

5. What’s the difference between *T and &T?

  • *T: Type declaration (pointer to type T).
  • &T: Operator to get the address of a variable.

6. Can you have a pointer to a pointer?

A: Yes (double pointers):

var x int = 10 ptr := &x pptr := &ptr // **int fmt.Println(**pptr) // 10

7. When to use pointers vs values in function arguments?

  • Pointers: To modify the original value or for large structs.
  • Values: For small, immutable data or read-only operations.

8. What happens if you dereference a nil pointer?

A: Runtime panic:

var ptr *int fmt.Println(*ptr) // panic: nil pointer dereference

9. How do pointers work with slices and maps?

  • Slices/maps are already reference-like (no need for pointers usually).
  • But pointers are needed if you want to modify the slice/map header itself.

10. What’s the output of this code?

func update(x *int) { *x = 20 } func main() { x := 10 update(&x) fmt.Println(x) // ? }

A: 20 (pointer modifies the original value).

Key Points to Remember

  1. & → Address-of operator.
  2. * → Dereference operator (or pointer type declaration).
  3. Rule: *T is a pointer to type T.
  4. Always check for nil before dereferencing!