Pointers in Go

Contents
& takes the address
* follows it
Pointers as parameters
When pointers pay off

& takes the address

Every variable lives at an address in memory, and the & operator reveals it as a pointer value. Storing that pointer lets other code find and share the same variable instead of a copy.

package main import "fmt" func main() { x := 10 p := &x fmt.Println(*p) fmt.Println(&x == p) }

go run main.go

10
true

Printing a pointer shows a machine address, so examples here print the pointed to value or a comparison instead. For a deeper reference with extra examples, see the article on pointers.

* follows it

The * operator follows a pointer back to the variable it points at. Reading *p loads the stored value, and assigning to *p rewrites the original variable through the pointer.

package main import "fmt" func main() { x := 10 p := &x fmt.Println(*p) *p = 20 fmt.Println(x) }

10
20

The star has two jobs that context keeps apart: declaring a pointer type, as in *int, and following a pointer value, as in *p. Read the declaration as pointer to int and the expression as value at p.

Pointers as parameters

Function arguments arrive as copies, so a function that must change the caller variable takes a pointer instead. The caller passes &x and the function writes through its parameter.

package main import "fmt" func double(n *int) { *n = *n * 2 } func main() { x := 21 double(&x) fmt.Println(x) }

42

Without the pointer, double would only change its own local copy and main would still print 21. Choose pointer parameters only for functions that truly need to mutate or to avoid copying.

When pointers pay off

Pointers pay off in three places: sharing large structs without copying them, letting functions update shared state, and expressing absence with nil. Everywhere else plain values keep programs simpler.

package main import "fmt" type User struct { Name string Age int } func birthday(u *User) { u.Age++ } func main() { u := User{Name: "Ada", Age: 35} birthday(&u) fmt.Println(u) }

{Ada 36}

A nil pointer points nowhere, so always check for nil before following one that might be empty. Go keeps pointer arithmetic out of the language, which removes a whole family of memory bugs.

Next: Structs

Article author: Arthur Isaev

Related articles
Golang
Go course: zero to hero in 50 lessons
What is Go and where it runs
Install Go and check the version
Your first Go program
Modules with go mod
Variables
Constants
Types
Numbers
Strings
Conditions with if
Switch in Go
Loops with for
Arrays in Go
Slices in Go
Maps in Go
Functions in Go
Errors as values
Pointers in Go
Structs in Go
Methods in Go
Interfaces in Go
Embedding instead of inheritance
Generics in Go
Packages and imports
The go toolchain
Testing with go test
Advanced errors
defer in Go
Strings, bytes and runes
Time in Go
JSON
Files and IO
HTTP servers
HTTP clients
Context
Goroutines
Channels
select and sync
CLI: args, flags, env
Logging
Regular expressions
Sorting
Concurrency patterns
Databases with database/sql
Benchmarks and profiling
Toolchain and CI
Project layout
Capstone: wordfreq CLI
Capstone: JSON API server
Hero roadmap: the whole course on one page
Install Go on Windows 11
Install Go on Ubuntu
Install Go on Rocky Linux
Install Go on macOS
Install Go on FreeBSD
Go in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: