Golang Pointers For Complete Beginners.
If you’ve just started learning Go (or Golang) and you keep hearing about “pointers” and “memory,” don’t panic.
They sound complex, but once you understand how computers store and share data, you’ll see that pointers are actually super simple — and super powerful.
Let’s go step by step 🚶♂️
What Is Memory, Anyway?
When your Go program runs, every variable you create (like numbers, strings, or structs) gets stored somewhere in the computer’s memory (RAM).
Imagine memory as a huge set of labeled boxes 📦.
Each box has:
- an address (like
0x140001220) - and a value inside it.
When you write this in Go:
package main
import "fmt"
func main() {
age := 17
fmt.Println(age)
}
The computer creates a memory box for age and puts the number 17 inside. But what if we want to see where that box lives in memory? Let’s find out 👇
Getting a Variable’s Address (& Operator).
In Go, you can get the memory address of a variable using the & operator.
package main
import "fmt"
func main() {
age := 17
fmt.Println("Age:", age)
fmt.Println("Memory Address:", &age)
}
Output:
Age: 17
Memory Address: 0xc0000140a8
That weird number (0xc0000140a8) is the memory address where Go stored your variable. Think of it like the house address for age.
What Is a Pointer?
A pointer is just a variable that stores a memory address — not the actual value. So instead of storing the number 17, a pointer stores where the number 17 is located. Here’s how you create one:
package main
import "fmt"
func main() {
age := 17
var pointerToAge *int = &age // pointer to an int
fmt.Println("Pointer:", pointerToAge)
}
pointerToAge is a pointer to an integer (*int). It “points” to the location of age in memory.
Getting the Value a Pointer Points To (* Operator).
Now that we have a pointer, how do we get the actual value? Use the * operator again — this time to dereference the pointer.
package main
import "fmt"
func main() {
age := 17
pointerToAge := &age
fmt.Println("Pointer:", pointerToAge)
fmt.Println("Value at pointer:", *pointerToAge)
}
Output:
Pointer: 0xc0000140a8
Value at pointer: 17
So:
&age → get address of age
*pointerToAge → get value at that address
That’s it. You’ve mastered the heart of pointers 🔥
Changing Values Through Pointers.
Pointers are not just for reading — you can also use them to update variables indirectly.
package main
import "fmt"
func main() {
age := 17
pointerToAge := &age
*pointerToAge = 18 // changes the original variable
fmt.Println("Age after update:", age)
}
Output:
Age after update: 18
When you changed the value at pointerToAge, it updated age — because both point to the same memory box!
Why Use Pointers?
Here are three real reasons you’ll use pointers in Go:
-
To avoid copying large data. Passing large structs or arrays by value can be slow — pointers let you pass references instead.
-
To modify data inside functions. Without pointers, Go functions get copies of values, not the original ones.
-
To work with complex data (e.g., structs, slices, maps). Many Go features rely on pointers behind the scenes.
Example: Passing Values vs Pointers.
Without Pointers (copy).
func increase(n int) {
n = n + 1
}
func main() {
num := 10
increase(num)
fmt.Println(num) // still 10
}
increase() got a copy of num, so the original value didn’t change.
With Pointers (reference).
func increase(n *int) {
*n = *n + 1
}
func main() {
num := 10
increase(&num)
fmt.Println(num) // now 11
}
By passing &num, we gave the function access to the actual memory box.
✍️ Written by Moses Kisakye
- Published on October 27, 2025
