Cloud Explainers
Home › Go (Golang) › Golang Idioms for Beginners, episode 1

Golang Pointers in 5 Minutes

Go (Golang)5 min video

Go pointers for beginners in under five minutes, on a real Go program: why a function that changes a struct leaves the caller's copy untouched, what *T and & mean, how a pointer receiver lets rex.Walk() change rex, why a missing map entry gives a nil pointer and a panic, and why returning the address of a local variable is fine in Go.

Two walks, zero counted

You pass a struct to a Go function, the function changes it, and back in main, nothing changed. Trish runs a dog-walking business. Her dog Rex went out twice, so addWalk ran twice, and her program prints: Rex walks, zero. In under five minutes, you'll fix it with two small symbols, and see the pointer mistake that crashes Go programs.

Trish's laptop: ~/walks
sed -n '10,19p' main.gofunc addWalk(d Dog) {
    d.Walks++
}

func main() {
    rex := Dog{Name: "Rex"}
    addWalk(rex)
    addWalk(rex)
    fmt.Println(rex.Name, "walks:", rex.Walks)
}
Trish's laptop: ~/walks
go run .Rex walks: 0

Go hands over a copy

Here's why. In Go, every argument is copied. Call addWalk with rex, and the function gets its own copy of the whole dog. It adds one to the copy, then the copy is thrown away. The second call: a fresh copy, the same. The rex in main was never touched.

Every call gets its own copy of rex: diagram of main's rex, copy 1, copy 2

You can see the copies. Inside addWalk, the dog is called d. Trish prints where each one lives in memory, its address. Guess first: same address, or different? Different, and new on each call: a fresh dog for every call, while Rex stays at zero.

Trish's laptop: ~/walks
go run .main's rex:   0x42a7c3c2c030
addWalk's d: 0x42a7c3c2c048  walks 1
addWalk's d: 0x42a7c3c2c060  walks 1
Rex walks: 0

Think of hiring a painter. Hand them a photo of your house, and they paint the photo. Your house stays the same color. Hand them your street address, and they go to the real house. A pointer is that address.

A photo of your house vs its address: comparison table of Give the painter a photo vs Give the painter the address, They get, They paint, In Go

The fix: pass the address

So Trish passes the address. In the function, the type becomes *Dog: a star before a type means a pointer to a Dog. In main, she passes &rex: an ampersand before a variable gives its address. The line that printed d's address loses its ampersand too, because d is already an address.

Pass the address, not a copy: code change of main.go, func addWalk(d *Dog) { (*Dog: a pointer to a Dog), addWalk(&rex) (&rex: rex's address), addWalk(&rex), fmt.Printf("addWalk's d: %p walks %d\n", d, d.Walks)

Run it again. Now addWalk's d has the same address as main's rex, on both calls. The walks go to one, then two, and Rex walks: two.

Trish's laptop: ~/walks
go run .main's rex:   0x4ad5c9ad0048
addWalk's d: 0x4ad5c9ad0048  walks 1
addWalk's d: 0x4ad5c9ad0048  walks 2
Rex walks: 2

& and * on their own

Forget the dog for a moment: here's a plain number called walks, set to five. p is ampersand walks: its address. Then: star p equals six. A star before a pointer means go to that address, so walks is now six. A star before a type, like star int, means a pointer to an int, and one that points at nothing yet is nil.

Trish's laptop: ~/walks
sed -n '6,12p' main.go    walks := 5
    p := &walks
    fmt.Println(p)
    *p = 6
    fmt.Println(walks)
    var q *int
    fmt.Println(q)
Trish's laptop: ~/walks
go run .0x3dda64c1c008
6
<nil>

Methods take the address for you

Real Go code rarely passes ampersands by hand. The star goes on a method instead: a function attached to a type. The bit in brackets before the name, d *Dog, is called the receiver, and here it's a pointer. Inside, d.Walks needs no star: Go follows the pointer for a dot by itself. Trish writes rex dot Walk on a plain Dog, and Go takes the address for her: two walks.

Trish's laptop: ~/walks
sed -n '10,18p' main.gofunc (d *Dog) Walk() {
    d.Walks++
}

func main() {
    rex := Dog{Name: "Rex"}
    rex.Walk()
    rex.Walk()
    fmt.Println(rex.Name, "walks:", rex.Walks)
Trish's laptop: ~/walks
go run .Rex walks: 2

Effective Go, the Go team's own guide, says it plainly: if the value lives in a variable, Go adds the ampersand for you. A method that changes its value gets a star receiver: the pointer you'll write most.

Methods take the address for you: browser screenshot from the video

When a pointer is nil

Here's the crash. Trish keeps her dogs in a map of pointers, looked up by name. Her helper Mike books a walk for Max, who isn't in it. Guess first: what happens? It crashes. A missing key hands back nil, an address to nowhere, and Walk follows it to no dog: nil pointer dereference.

Trish's laptop: ~/walks
sed -n '15,17p' main.go    dogs := map[string]*Dog{"Rex": {Name: "Rex"}, "Bella": {Name: "Bella"}}
    dogs["Rex"].Walk()
    dogs["Max"].Walk()
Trish's laptop: ~/walks
go run .panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0xa2198]

goroutine 1 [running]:
main.(*Dog).Walk(...)
    /home/trish/walks/main.go:11
main.main()
    /home/trish/walks/main.go:17 +0x178
exit status 2

The fix: check before you follow it. Trish asks the map for two things: the dog, and a true or false called ok, which says whether Max is there at all. If not, she says so and stops. A pointer you got any other way: compare it with nil first.

Trish's laptop: ~/walks
sed -n '16,21p' main.go    d, ok := dogs["Max"]
    if !ok {
        fmt.Println("no dog called Max")
        return
    }
    d.Walk()
Trish's laptop: ~/walks
go run .no dog called Max

Returning a pointer

When a function ends, its variables are cleared away. So what if it hands back the address of one? NewDog does exactly that. Go's compiler notices, and moves d to the heap, memory that stays around after the function ends. Build with a flag that makes the compiler explain itself, and it prints: moved to heap, d. Max keeps his walk, and that's the New function you'll see in Go packages everywhere.

Trish's laptop: ~/walks
sed -n '10,19p' main.gofunc NewDog(name string) *Dog {
    d := Dog{Name: name}
    return &d
}

func main() {
    max := NewDog("Max")
    max.Walks++
    fmt.Println(max.Name, "walks:", max.Walks)
}
Trish's laptop: ~/walks
go build -gcflags=-m . 2>&1 | grep moved./main.go:11:2: moved to heap: d
Trish's laptop: ~/walks
go run .Max walks: 1

So when do you use one? When the value must change. When a struct is big and copying it is waste. When nil means something, like no dog found. A small value you only read? Just copy it.

When to use a pointer: cards of It must change, It's big, nil means none, Skip it

Recap

So: Go copies every argument. A star type holds an address, an ampersand takes one, and a pointer can be nil, so check it. Give the painter the address, not the photo.

Pass a copy, or pass the address: diagram of main's rex, addWalk(rex), addWalk(&rex)

If pointers make sense now, a like helps YouTube show this to more beginners, and subscribing brings the next Go idiom: error return values.

Every command here ran for real, and the lab files are linked in the description. Thanks for watching Cloud Explainers.

Sources

Demos: one container on our own Mac standing in for a laptop; the dog-walking business is made up. Every command, error and number was real. Watch the full video on YouTube.

More videos

Python8 min

How to Fix Python ModuleNotFoundError: No Module Named

Kubernetes8 min

How to Fix a Kubernetes Pod Stuck in CrashLoopBackOff

Homelab & self-hosting9 min

How to Fix a Homelab ESXi Host Not Responding

PostgreSQL24 min

PostgreSQL: Full Tutorial for Beginners

Load balancing & Nginx9 min

How a Load Balancer Works with HAProxy

Python9 min

How Python Virtual Environments Work

Homelab & self-hosting9 min

How to Fix Cloudflare Tunnel Errors in a Homelab

APIs9 min

How to Fix API CORS Errors: No 'Access-Control-Allow-Origin'

Auth (OAuth, JWT)9 min

How Authenticator App Login Codes Work (TOTP)

Git22 min

Git and GitHub: Full Tutorial for Beginners

Homelab & self-hosting10 min

How a Homelab Hypervisor Works (VMware ESXi)

Kubernetes9 min

How to Fix Kubernetes ImagePullBackOff Errors

DNS8 min

How to Fix DNS 'Temporary Failure in Name Resolution' on Linux

AWS8 min

How to Fix AWS CLI 'Unable to Locate Credentials'

Homelab & self-hosting10 min

Proxmox vs ESXi for Your Homelab: When Each One Wins

PostgreSQL10 min

PostgreSQL UUID vs Auto Increment: Which Primary Key Should You Use?

Kubernetes21 min

Kubernetes: Full Tutorial for Beginners

Networking9 min

How TCP Sockets Work

PostgreSQL7 min

How to Fix PostgreSQL 'Could Not Connect to Server'

Claude & the Claude Developer Platform9 min

Claude Code Hooks Will Finally Make Sense After This

DevOps & CI/CD9 min

DevOps with Ansible Will Finally Make Sense After This

Networking9 min

How SNMP Network Monitoring Works

Git8 min

How to Fix GitHub 'Permission Denied (publickey)' Errors

Docker21 min

Docker: Full Tutorial for Beginners

PostgreSQL7 min

How to Fix PostgreSQL 'Password Authentication Failed'

Linux8 min

Why One Linux Server Handles More Users Than You Think

Networking7 min

How to Fix Network 'Connection Refused' Errors

DevOps & CI/CD9 min

DevOps Pipelines: Jenkins vs GitHub Actions

Linux11 min

Linux Nginx Reverse Proxy: Full Tutorial for Beginners

Networking11 min

Every Networking Term a Beginner Needs in 12 Minutes

Docker6 min

How to Fix Docker 'Permission Denied' on docker.sock

Git7 min

How to Fix Git 'Updates Were Rejected' When You Push

Linux7 min

How to Fix nginx 502 Bad Gateway Errors on Linux

Linux8 min

How to Fix Linux apt 'Could Not Get Lock' Errors (Ubuntu)

Networking9 min

How Email Works on the Internet (SMTP) in 10 Minutes

Python8 min

The Python Roadmap for Beginners

Go (Golang)21 min

Golang REST API with Gin: Full Tutorial for Beginners

Networking10 min

How a WireGuard VPN Network Works

Linux9 min

Linux Makefiles Will Finally Make Sense After This

Auth (OAuth, JWT)9 min

SAML vs OAuth vs OpenID Connect: What's the Difference?

Python7 min

How to Fix Python cron Jobs That Never Run

Linux22 min

Linux Bash Scripting: Full Tutorial for Beginners

Networking10 min

How PXE Network Boot Works

Active Directory9 min

How Active Directory Works

Linux10 min

How to Fix a Linux systemd Service That Won't Start

Linux9 min

Linux Logs: syslog vs journald

Auth (OAuth, JWT)9 min

JWT vs Session Cookies: Which One Should You Use?

Docker10 min

Docker vs Docker Compose: Which One Should You Use?

Docker9 min

How to Fix Docker Compose Containers That Can't Talk to Each Other

Django (Python web framework)22 min

Django Full Tutorial for Beginners

Linux9 min

How Linux systemd Works

Networking8 min

How to Fix 'Windows Cannot Access' Network Share Errors (SMB)

APIs10 min

REST vs gRPC: Which One Should You Use?

Certificates & PKI (ADCS)9 min

How to Fix SSL Certificate Chain Errors (Missing Intermediate)

Python9 min

Every Python Concept for Beginners in 10 Minutes

Databases & SQL9 min

Every SQL Join Type in 10 Minutes

OpenRouter (one API for many AI models)9 min

Why OpenRouter Free Models Hit Their Limit So Fast

Gemini API & Google AI Studio9 min

Why Your Gemini API Free Tier Runs Out So Fast

OpenRouter (one API for many AI models)10 min

OpenRouter Will Finally Make Sense After This

Caching & Redis10 min

How Redis Caching Works

Gemini API & Google AI Studio10 min

Gemini API vs Vertex AI: Which One Should You Use?

Gemini API & Google AI Studio8 min

How to Fix a Leaked Gemini API Key

Go (Golang)9 min

Why Golang Uses Goroutines Instead of Threads

Gemini API & Google AI Studio9 min

How to Fix Gemini API Error 429: Resource Exhausted

Claude & the Claude Developer Platform10 min

Claude Token Pricing Will Finally Make Sense After This

Python11 min

Why Python Web Scraping Returns Nothing (and the Fix)

Python10 min

Python uv vs pip: Which One Should You Use?

Python22 min

Python Automation: Full Tutorial for Beginners

Auth (OAuth, JWT)9 min

How OAuth Login Works

Gemini API & Google AI Studio10 min

Google AI Studio Will Finally Make Sense After This

Docker10 min

What Happens When You Run a Docker Container

Claude & the Claude Developer Platform8 min

How to Fix Claude Code Billing Your API Key Instead of Your Plan

Auth (OAuth, JWT)9 min

Why Passkeys Can't Be Phished Like Passwords

PowerShell10 min

PowerShell vs Bash: Which One Should You Learn?

Linux10 min

The Linux Roadmap for Beginners

Django (Python web framework)9 min

Django vs FastAPI: Which One Should You Use?

Databases & SQL10 min

Why SQLite Is the Most Used Database in the World

Databases & SQL11 min

How a NoSQL Database Like MongoDB Works

Python11 min

How to Fix Python pip error: externally-managed-environment

Databases & SQL9 min

How to Fix MySQL Too Many Connections Database Errors

Go (Golang)9 min

Golang Concurrency Will Finally Make Sense After This

AWS10 min

How Amazon S3 Works

Kubernetes10 min

How Kubernetes Works

Git10 min

How Git Works

Docker10 min

How to Fix Docker Port Already Allocated Errors

PostgreSQL10 min

How PostgreSQL Works

Git8 min

How to Fix Git Merge Conflicts

Linux9 min

How to Fix No Space Left on Device in Linux

Networking10 min

How the Internet Works

Databases & SQL9 min

SQL vs NoSQL: When Each One Wins

Auth (OAuth, JWT)8 min

How to Fix JWT Token Expired Errors (401 Unauthorized)

Claude & the Claude Developer Platform9 min

Claude API vs Claude Pro: Why Your Subscription Doesn't Include It

AI agent skills9 min

Why AI Agent Skills Beat Giant Prompts

Linux10 min

Linux Cron Jobs Will Finally Make Sense After This

Cybersecurity10 min

Security Logs Will Finally Make Sense After This

Cybersecurity8 min

How to Fix a Security Leak: An API Key Pushed to GitHub

DNS9 min

DNS Will Finally Make Sense After This

Cybersecurity9 min

Never Ignore a Security Update

Auth (OAuth, JWT)9 min

Two-Factor Authentication Will Finally Make Sense

Networking8 min

Never Expose a Database to the Internet

Cybersecurity10 min

SSH Keys vs Passwords: Which Is Safer for Security?

Linux10 min

The 5 Levels of Linux Boot: From Power On to Login

Networking12 min

The 5 Levels of Network Firewalls

Cybersecurity10 min

Password Security: Why Hackers Crack Weak Passwords

Linux9 min

Why Your Linux Server Runs Out of Memory

Networking10 min

HTTP vs HTTPS: What Actually Changes on Your Network

Linux12 min

Every Linux File Permission You Need to Know

Linux10 min

Linux Processes vs Threads: When Each One Wins

Networking11 min

The OSI Model Will Finally Make Sense After This

Git11 min

The 7 Levels of Git

Networking10 min

Every Network Troubleshooting Command You Need

Cybersecurity10 min

Hashing vs Encryption in Cybersecurity: When Each One Wins

Networking11 min

Why the Internet Ran Out of IP Addresses

Linux11 min

Linux Package Managers: apt vs Snap vs Flatpak

Python9 min

Why Python Is Slow (and When It Doesn't Matter)

Networking11 min

TCP vs UDP: When Each One Wins

Linux8 min

Stop Running Linux as Root (Do This Instead)

Linux11 min

Linux Users and Groups Will Finally Make Sense

Cybersecurity9 min

Why Phishing Beats Good Security (and How to Spot It)

Docker12 min

The 7 Levels of Docker

AI agents & MCP12 min

AI Agents Will Finally Make Sense After This

Git8 min

Stop Using git push --force (Use This Instead)

AI agent skills10 min

Agent Skills vs MCP: When Each One Wins

Networking12 min

Networking Will Finally Make Sense After This

Linux12 min

Every Linux Command You Actually Need

Cybersecurity12 min

The 7 Levels of Cybersecurity

System design12 min

Every System Design Concept You Need, in One Diagram

PostgreSQL10 min

Why PostgreSQL Ignores Your Index