← Back to Go Course | Chapter 8: Pointers | Lesson 3 of 6

Pointers to Structs

A pointer to a struct is a shortcut note that lets any function reach the exact same original form, instead of everyone scribbling on their own separate photocopy.
Syntax
go
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

markup
// 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)
}

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

markup
// 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)
}

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

markup
// 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)
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Passing a large struct by value to many functions, causing unnecessary copying, when a pointer would be more efficient.
  2. Using a value receiver method when the goal is actually to modify the struct's own fields through a method call.
  3. Forgetting struct pointer literals use &Type{...} syntax to create and get the address in one step.
Chapter Summary
  • &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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.