Pointers In Go
2025-Sep-25
If you're new to Go, pointers can seem difficult at first. But in reality, they're a powerful and essential tool once you get the hang of them. I hope that, with this blog post, you'll get a good starting point for understanding what they are and how to use them.
The basics of pointers
To understand pointers, you'll first have to understand what a pointer is. A pointer is a variable that stroes the memory address of another variable. In this sense, a pointer is a data type that maps to another variable's memory address. Why use pointers? With a pointer variable, you can retrive or change the value of a specific variable. This makes your program memory-efficient.
Pointers specifically to Go
In Go, there are two operators you need to know about in regards to pointers. The first one is the *, which signifies a pointer type or is used to dereference a pointer. The second operator is &, which returns the memory address of a value. Those are the only two operators you need to know about.
You use these operators differently, and a way to think about it is with the equation * = &. This raises the question: why not just use &? The anwser is straightforward, and I touched on it earlier in this post. The * operator allows for referencing a value's address but also for dereferencing an address to get its value. Take a look at the examples below. Here you'll see example #1 which does not use pointers, and example #2 which uses pointers:
Example #1
func addOne(number int) {
number = number + 1
fmt.Println("Value inside addOne function:", number)
}
func main() {
var number int = 10
fmt.Println("Initial value:", number)
addOne(number)
fmt.Println("Final value:", number)
}
Example #2
func addOne(number *int) {
*number = *number + 1
fmt.Println("Value inside addOne function:", *number)
}
func main() {
var number int = 10
fmt.Println("Initial value:", number)
addOne(&number)
fmt.Println("Final value:", number)
}
If you try to run this on The Go Playground, the difference is clear. Example #2 demonstrates why we use both * and &. First, you use the & operator to get the memory address of a variable and pass it to a function. Then, within the function, you use the * operator to dereference that address, which allows you to access or change the original variable's value. Essentially, you're passing the location of a variable as a vlaue, and the function uses that location to find and work with the original variable's value.
The difference between pointers in languages
This section is for those curious about the differences between pointers in Go and other languages, like C or C++. In short, pointer arithmetic refers to performing arithmetic operations on pointers. For example, in C, you can increment or decrement a pointer to move to the next memory address, or you can compare pointers to check their relative positions. There are many different operations, but I won't go into detail as that's a topic for another post.
Pointer Arithmetics in C with Examples A Comprehensive Guide to Pointers in Go you will never ask about pointers again after watching this video Pointers are easy! Pointers