Pointers to Structs
In this page:
p := &StructType{Field: value}
p.Field = other // auto-dereferenced
Creating a Pointer to a Struct
Prefixing a struct literal with & both creates the struct value and gives you a pointer to it in a single expression, which is extremely common when a function is meant to return an object that will be mutated later.
Example: Creating a Pointer to a Struct
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define a struct type named Point
type Point struct {
X, Y int
}
// main is where the program starts running
func main() {
// Short declaration: create p and infer the type
p := &Point{X: 1, Y: 2}
// Print the values followed by a newline
fmt.Println(p)
}
Login to try C/C++/Java/PHP code in the editor
Passing Struct Pointers to Functions
Passing a pointer to a struct, rather than the struct itself, lets the called function modify the caller's original data and avoids copying potentially large structs on every call.
Example: Passing Struct Pointers to Functions
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define a struct type named Account
type Account struct {
Balance float64
}
// Define the function deposit
func deposit(a *Account, amount float64) {
a.Balance += amount
}
// main is where the program starts running
func main() {
// Short declaration: create acc and infer the type
acc := Account{Balance: 100}
deposit(&acc, 50)
// Print the values followed by a newline
fmt.Println(acc.Balance)
}
Login to try C/C++/Java/PHP code in the editor
Field Access via Pointer
Accessing and setting fields through a struct pointer uses the exact same dot notation as a plain struct value, since Go automatically dereferences the pointer for field access.
Example: Field Access via Pointer
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define a struct type named Book
type Book struct {
Title string
Copies int
}
// main is where the program starts running
func main() {
// Short declaration: create b and infer the type
b := &Book{Title: "Go Basics", Copies: 3}
b.Copies--
// Print the values followed by a newline
fmt.Println(b.Title, "copies left:", b.Copies)
}
Login to try C/C++/Java/PHP code in the editor
- Passing a large struct by value to many functions, causing unnecessary copying, when a pointer would be more efficient.
- Using a value receiver method when the goal is actually to modify the struct's own fields through a method call.
- Forgetting struct pointer literals use &Type{...} syntax to create and get the address in one step.
- &Type{...} creates a struct and immediately returns a pointer to it.
- Passing a struct pointer to a function lets that function modify the original struct's fields.
- Field access on a struct pointer uses the same dot syntax as a plain struct value.
- Pointer receivers on methods are the standard way to let methods mutate a struct's own state.
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: