Introduction
A pointer is a value that stores the memory address of another variable rather than the value itself. Go supports pointers for controlled, safe indirection: you can take the address of a variable, pass it around, and dereference it to read or modify the original value, all without the pointer arithmetic found in languages like C. Pointers are essential for mutating data across function boundaries and for avoiding expensive copies of large structs.
Cricket analogy: Like a scoreboard operator holding a note with the location of the official scorebook rather than a photocopy, updating through that note changes the real book directly, without the risky "walk forward three pages" arithmetic C allows.
Syntax
x := 42
p := &x // p is a *int holding the address of x
fmt.Println(*p) // dereference: prints 42
*p = 100 // modifies x through the pointer
// new(T) allocates zeroed memory and returns a pointer to it
count := new(int) // *int, points to 0
*count = 5Explanation
The & operator produces a pointer to a variable, and the * operator, when applied to a pointer, dereferences it to access or modify the pointed-to value. p := &x makes p a *int referring to x's address; *p = 100 writes through that pointer, changing x itself. new(T) allocates zeroed storage for a value of type T and returns a pointer to it, which is a shorthand alternative to declaring a variable and taking its address. Unlike C, Go does not allow pointer arithmetic (you cannot do p + 1 to walk through memory), which eliminates an entire category of memory-safety bugs. Dereferencing a nil pointer causes a runtime panic, so code must ensure a pointer is non-nil before using *p.
Cricket analogy: Like & giving you the exact locker location of Kohli's real bat (p := &bat) and *p = "new grip" regripping the actual bat itself, new(Bat) hands you a fresh, unassigned locker, but checking an empty locker before use (nil pointer) causes the whole operation to collapse.
Example
package main
import "fmt"
type Account struct {
Balance int
}
func deposit(a *Account, amount int) {
a.Balance += amount
}
func main() {
acc := Account{Balance: 100}
deposit(&acc, 50)
fmt.Println("balance:", acc.Balance)
var np *Account
if np == nil {
fmt.Println("np is nil, safe to check before dereferencing")
}
}Output
balance: 150
np is nil, safe to check before dereferencingKey Takeaways
- & takes the address of a variable; * dereferences a pointer to read or write the pointed-to value.
- new(T) allocates zeroed memory for T and returns a *T.
- Go has no pointer arithmetic, unlike C, which removes a common source of memory bugs.
- Passing a pointer to a function lets it mutate the caller's data and avoids copying large structs.
- Dereferencing a nil pointer causes a runtime panic, so nil checks matter before use.
Practice what you learned
1. What does the expression &x produce in Go?
2. What does new(int) return?
3. Why does Go not allow pointer arithmetic like p + 1?
4. What happens if you dereference a nil pointer in Go?
5. In the deposit(a *Account, amount int) example, why does passing &acc allow the function to modify the caller's struct?
Was this page helpful?
You May Also Like
Structs in Go
A composite type that groups related fields together, forming the basis for custom data modeling in Go.
Arrays in Go
A fixed-size, ordered collection of elements of the same type whose length is part of its type.
Functions in Go
Learn how to declare, call, and use functions as first-class values in Go.
Methods in Go
Learn how to attach methods to types in Go using value and pointer receivers.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics