Pointers in Go
Pointers in Go allow a program to work directly with memory addresses. They are used to share data between functions, improve performance, and modify values in place.
Unlike some languages, Go keeps pointers simple and safe. There is no pointer arithmetic, which prevents many common memory bugs.
Why Pointers Are Important
- Avoid unnecessary data copying
- Modify values inside functions
- Efficient memory usage
- Essential for structs and methods
Understanding Memory and Addresses
Every variable in Go is stored at a specific memory location. A pointer stores the address of that location.
The & operator gives the memory address of a variable.
package main
import "fmt"
func main() {
x := 42
fmt.Println("Value:", x)
fmt.Println("Address:", &x)
}
Here, &x returns the memory address where x is stored.
Declaring Pointer Variables
A pointer is declared using the * symbol before the type.
var p *int
This declares a pointer that can store the address of an integer.
Assigning a Pointer
You can assign a pointer to the address of a variable.
package main
import "fmt"
func main() {
value := 100
var ptr *int
ptr = &value
fmt.Println("Value:", value)
fmt.Println("Pointer:", ptr)
}
Now ptr points to the same memory location as value.
Dereferencing a Pointer
Dereferencing means accessing the value stored at the memory address.
This is done using the * operator.
fmt.Println(*ptr)
This prints the value stored at the address held by the pointer.
Modifying Values Using Pointers
One of the biggest advantages of pointers is modifying data directly.
package main
import "fmt"
func updateValue(n *int) {
*n = *n + 50
}
func main() {
number := 25
updateValue(&number)
fmt.Println("Updated Value:", number)
}
The function modifies the original variable without returning anything.
Pointers vs Value Passing
Without pointers, Go passes copies of values to functions. With pointers, functions operate on the original data.
func changeValue(n int) {
n = 100
}
func changeWithPointer(n *int) {
*n = 100
}
Only the pointer-based function changes the original value.
Nil Pointers
A pointer that does not point to any address is called a nil pointer.
Using a nil pointer causes a runtime panic.
var ptr *int
fmt.Println(ptr) // nil
Always check for nil before dereferencing pointers.
Real-World Example: Updating Account Balance
Pointers are widely used in financial and data-processing systems.
func deposit(balance *float64, amount float64) {
*balance += amount
}
func main() {
balance := 1500.75
deposit(&balance, 499.25)
fmt.Println("Final Balance:", balance)
}
This approach avoids copying large data structures.
Common Mistakes
- Dereferencing a nil pointer
- Confusing
*for declaration vs dereferencing - Overusing pointers when not needed
Best Practices
- Use pointers only when modification is required
- Keep pointer usage readable
- Always validate nil pointers
- Prefer simplicity over micro-optimization
What’s Next?
In the next lesson, you will learn about Structs in Go, which allow you to group related data together and are often used with pointers in real applications.