How to Create a Pointer to a Struct in Go
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
<p> := &<Type>{<fields>}
func <name>(<param> *<Type>) {
<param>.<Field> = <value>
}
| Part | What 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 runcompiles and runs a single file. This guide uses Go 1.27.4.
Step 1: Write the Code
Create a file named 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
go run main.go
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
| Mistake | What happens | Do this instead |
|---|---|---|
| Passing the struct instead of a pointer | The function changes a copy, and your struct stays the same | Take *Counter and pass a pointer |
Using a pointer that was never set, as in var c *Counter | panic: runtime error: invalid memory address or nil pointer dereference | Create the struct with &Counter{} |
Writing (*c).Count everywhere | It works, but it's noise | Write c.Count; Go dereferences for you |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A change made inside a function is lost | The function received a copy | Pass a pointer |
nil pointer dereference | The pointer is nil | Set 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
- Address operators, in the Go specification.
- A Tour of Go: Pointers to structs.
