Back to Course
Pointers & Memory

Pointers in Go – When and Why to Use Them (Memory Management)

What is a Pointer?

A pointer stores the memory address of a variable, letting you reference and modify the original value indirectly.

x := 10
p := &x       // p holds the address of x
fmt.Println(*p) // dereference: prints 10
*p = 20
fmt.Println(x)  // 20, x was modified through the pointer

Why Use Pointers?

  • To modify a variable's value inside a function (Go passes arguments by value by default).
  • To avoid copying large structs when passing them to functions, improving performance.
  • To represent optional/nullable values, since a pointer can be nil.

Pointers with Functions

func increment(n *int) {
    *n = *n + 1
}

x := 5
increment(&x)
fmt.Println(x) // 6

Unlike C, Go doesn't support pointer arithmetic, which makes pointers in Go safer to use.

Ready to master real-world Go skills?

Learn Go hands-on with mentor-led, live sessions.

Explore Course