Pointers in Go
This module covers pointers in Go, including declaration, initialization, dereferencing, pointer operations, passing to functions, with structs and arrays, double pointers, and nil pointers.
1. What is a Pointer?
A pointer is a variable that stores the memory address of another variable. Pointers allow you to share and modify data efficiently, especially for large data structures.
Why Pointers?
Enable functions to modify variables outside their own scope.
Allow efficient passing of large structs or arrays (no copying).
Essential for building data structures like linked lists, trees, and graphs.
Real-World Analogy
A pointer is like a remote control: you can use it to change the channel (value) on a TV (variable) from a distance (another function).
2. Pointer Basics
var x int = 42
var ptr *int = &x
fmt.Println(ptr) // prints address
fmt.Println(*ptr) // dereferences pointer
*
is used to declare and dereference pointers.&
is used to get the address of a variable.
3. Modifying Values via Pointers
Changing the value at a pointer updates the original variable.
*ptr = 100
fmt.Println(x) // 100
4. Pointers and Functions
Pointers can be passed to functions to allow modification of the original variable.
func increment(num *int) {
*num++
}
Why not just return the value?
Sometimes you want to modify multiple variables or large data structures efficiently.
5. Pointers and Structs
Pointers to structs allow you to modify struct fields directly and efficiently.
6. Double Pointers
A pointer to a pointer (**int
) is rarely needed in Go, but can be useful for advanced scenarios (e.g., modifying a pointer itself).
7. Nil Pointers
Uninitialized pointers are nil
. Always check for nil
before dereferencing to avoid runtime panics.
8. Pointers and Arrays/Slices
You can point to array elements or the first element of a slice. Slices themselves are reference types, so you rarely need pointers to slices.
9. Memory Safety in Go
Go does not allow pointer arithmetic (unlike C/C++), making pointers safer and less error-prone.
The garbage collector manages memory, so you don’t need to free memory manually.
Pointers cannot point to stack-allocated variables that have gone out of scope.
10. Best Practices
Use pointers for large structs or when mutation is needed.
Avoid pointer arithmetic.
Always check for
nil
before dereferencing.Use value types for small structs and when mutation is not needed.
Go vs. Other Languages
Go’s pointers are safer than C/C++: no pointer arithmetic, no manual memory management.
No dangling pointers or buffer overflows due to Go’s memory model and garbage collector.
Go’s slices and maps are reference types, so you often don’t need pointers for efficient sharing.
Practice & Conceptual Questions
Write a function
swap(a, b *int)
that swaps two integers using pointers.Create a struct
Rectangle
with Width and Height. Write a method using pointer receiver to scale its size.Declare a nil pointer to a float64 and safely assign it a value inside a function.
Use a pointer to update the second element in a slice of strings.
Declare a variable of type pointer to pointer (
**int
) and print the dereferenced value.(Advanced) Create a function that takes a slice and modifies it using a pointer to the first element.
Why does Go disallow pointer arithmetic? What are the safety benefits?
Further Reading
Last updated