100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Go

Pointers in Go

A variable that holds the memory address of another value, enabling shared access and mutation without copying.

Data StructuresIntermediate9 min readJul 8, 2026
Analogies

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

go
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 = 5

Explanation

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

go
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

go
balance: 150
np is nil, safe to check before dereferencing

Key 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

Was this page helpful?

Topics covered

#Go#GoProgrammingStudyNotes#Programming#PointersInGo#Pointers#Syntax#Explanation#Example#StudyNotes#SkillVeris