DEV Community

Cover image for Mastering Pointers in Go: A Complete Beginner-to-Advanced Guide
Md Abu Musa
Md Abu Musa

Posted on

Mastering Pointers in Go: A Complete Beginner-to-Advanced Guide

Go (Golang) is a powerful, statically typed language built for simplicity, performance, and scalability. One of the key features that sets Go apart from many high-level languages is its support for pointers, which allow you to directly reference memory addresses.

In this blog, you’ll learn everything you need to know about pointers in Go β€” from what they are and why they matter, to how they’re used in real-world applications.


πŸ“Œ What is a Pointer?

A pointer is a variable that holds the memory address of another variable.

πŸ‘‰ Analogy:

Think of a variable as a house and its value as a person living inside. A pointer is like the house’s address written on a note. You don’t carry the person β€” you carry the address.


πŸ§ͺ Basic Pointer Syntax in Go

var x int = 10
var p *int = &x
Enter fullscreen mode Exit fullscreen mode
  • x is an int variable.
  • &x gives the memory address of x.
  • p is a pointer to int (*int) that stores that address.

πŸ” Dereferencing

To get the value stored at the pointer address:

fmt.Println(*p) // Output: 10
Enter fullscreen mode Exit fullscreen mode

βš™οΈ Why Use Pointers?

  • πŸ” Avoid copying large values (performance gain).
  • 🧠 Modify values inside functions.
  • βš’οΈ Work with dynamic data structures (like linked lists, trees).
  • πŸš€ Share state between functions or goroutines.

πŸ§‘β€πŸ’» Example: Modify Value via Pointer

func update(val *int) {
    *val = 99
}

func main() {
    num := 50
    update(&num)
    fmt.Println(num) // Output: 99
}
Enter fullscreen mode Exit fullscreen mode

βœ… *val = 99 changes the original num since we're working with its memory address.


πŸ†š Value vs Pointer

Feature Value (Copy) Pointer (Reference)
Memory usage More (copies value) Less (copies address)
Modify original? ❌ No βœ… Yes
Performance Slower with large data Faster

πŸ†• The new() Keyword

ptr := new(int)
*ptr = 77
fmt.Println(*ptr) // Output: 77
Enter fullscreen mode Exit fullscreen mode
  • new(int) allocates memory for an int and returns a pointer to it.
  • The pointer’s default value is zero.

πŸ‘€ Pointers with Structs

type User struct {
    Name string
}

func rename(u *User) {
    u.Name = "Alice"
}

func main() {
    user := User{Name: "Bob"}
    rename(&user)
    fmt.Println(user.Name) // Output: Alice
}
Enter fullscreen mode Exit fullscreen mode

βœ… Modifications inside the function persist outside because we pass a pointer.


🧠 Common Pitfalls

  1. ❌ Dereferencing a nil pointer
   var p *int
   fmt.Println(*p) // panic: runtime error: invalid memory address
Enter fullscreen mode Exit fullscreen mode
  1. βœ… Always check for nil:
   if p != nil {
       fmt.Println(*p)
   }
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Passing by Value vs Pointer (Recap)

Case Use Pointer?
Modify original data βœ… Yes
Pass large structs βœ… Yes
Read-only small values ❌ No

πŸ”š Conclusion

Pointers are a core concept in Go that unlock performance, control, and flexibility. By mastering pointers, you gain a deeper understanding of how data flows and memory works under the hood β€” critical skills for any serious Go developer.

✨ Remember: With great power (pointers), comes great responsibility (memory safety)!

Top comments (1)

Collapse
 
statikman profile image
statikMan

DON'T Dereferencing a NOT nil pointer