Skip to main content

How to Create a Pointer to a Struct in Go

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Go passes values by copying them, so a function that receives a struct works on its own copy. To let a function change your struct, pass a pointer to it: &Counter{} creates a struct and gives you its address in one step.

Syntax​

Syntax
<p> := &<Type>{<fields>}

func <name>(<param> *<Type>) {
<param>.<Field> = <value>
}
PartWhat it does
& before a struct literal, as in &Counter{}Creates a struct value and returns a pointer to it.
*<Type>The type of a pointer to that struct.
<param>.<Field>Reads or changes a field through the pointer; Go follows the pointer for you.

The Go specification: "For an operand x of type T, the address operation &x generates a pointer of type *T to x." It allows & directly on a composite literal such as &Counter{}.

The Example​

The example creates a counter, passes a pointer to it to increment twice, and prints the count.

Prerequisites​

  • Go. go run compiles and runs a single file. This guide uses Go 1.27.4.

Step 1: Write the Code​

Create a file named main.go:

main.go, which declares a Counter struct with Count, defines increment(c *Counter) that adds one to c.Count, creates a pointer to a new Counter, increments it twice and prints counter.Count.

main.go
package main

import "fmt"

type Counter struct {
Count int
}

func increment(c *Counter) {
c.Count++
}

func main() {
counter := &Counter{}
increment(counter)
increment(counter)
fmt.Println(counter.Count)
}

Step 2: Run It​

Terminal
go run main.go
Output
2

A zsh terminal where go run main.go prints 2.

Both calls changed the same struct through the pointer, so the count is 2. The command is the same in zsh, bash and PowerShell.

What Happens Without the Pointer​

With func incrementCopy(c Counter), which takes the struct itself, the function changes its own copy. I called it on a Counter{} and printed the count: 0.

Common Mistakes​

MistakeWhat happensDo this instead
Passing the struct instead of a pointerThe function changes a copy, and your struct stays the sameTake *Counter and pass a pointer
Using a pointer that was never set, as in var c *Counterpanic: runtime error: invalid memory address or nil pointer dereferenceCreate the struct with &Counter{}
Writing (*c).Count everywhereIt works, but it's noiseWrite c.Count; Go dereferences for you

Troubleshooting​

SymptomLikely causeFix
A change made inside a function is lostThe function received a copyPass a pointer
nil pointer dereferenceThe pointer is nilSet it with &Type{} before use

FAQ​

When should I use a pointer instead of a value?​

When a function must change the struct, or when the struct is large and copying it would be wasteful.

What about methods?​

The same rule applies: func (c *Counter) Increment() has a pointer receiver and can change the counter.

Conclusion​

&Type{} creates a struct and returns a pointer to it. Pass the pointer to functions that must change the struct, and read fields through it with the usual dot.

Additional Resources​