0
0
Goprogramming~10 mins

Struct pointers in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Struct pointers
Declare struct type
Create struct instance
Create pointer to struct
Access/modify fields via pointer
Use updated struct values
This flow shows how to declare a struct, create an instance, get a pointer to it, and access or change its fields through the pointer.
Execution Sample
Go
package main
import "fmt"

type Person struct {
    Name string
    Age  int
}

func main() {
    p := Person{"Alice", 30}
    pPtr := &p
    pPtr.Age = 31
    fmt.Println(p.Age)
}
This code creates a Person struct, gets a pointer to it, changes Age via the pointer, and prints the updated Age.
Execution Table
StepActionVariableValueExplanation
1Declare struct type PersonPersonstruct {Name string; Age int}Defines a new struct type with Name and Age fields
2Create instance ppPerson{Name:"Alice", Age:30}p holds a Person with Name Alice and Age 30
3Create pointer pPtr to ppPtr&ppPtr points to the memory address of p
4Modify Age via pointerpPtr.Age31Changes Age field of p through pPtr to 31
5Print p.AgeOutput31Shows updated Age value after modification
6EndProgram ends after printing updated Age
💡 Program ends after printing the updated Age value 31
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
pundefinedPerson{Name:"Alice", Age:30}Person{Name:"Alice", Age:30}Person{Name:"Alice", Age:31}Person{Name:"Alice", Age:31}
pPtrundefinedundefined&p&p&p
Key Moments - 2 Insights
Why does changing pPtr.Age also change p.Age?
Because pPtr points to p's memory, modifying pPtr.Age changes the actual data in p, as shown in step 4 and confirmed by the output in step 5.
Can we access struct fields directly through a pointer without explicit dereferencing?
Yes, Go lets you use pPtr.Age instead of (*pPtr).Age, making code simpler. This is shown in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of p.Age after step 4?
A30
B0
C31
Dundefined
💡 Hint
Check the 'Value' column for variable p after step 4 in the execution_table.
At which step is the pointer pPtr created?
AStep 3
BStep 4
CStep 2
DStep 1
💡 Hint
Look for the action 'Create pointer pPtr to p' in the execution_table.
If we changed pPtr.Age to 35 instead of 31, what would be printed at step 5?
A31
B35
C30
DError
💡 Hint
Changing pPtr.Age updates p.Age because pPtr points to p, see variable_tracker changes.
Concept Snapshot
Struct pointers in Go:
- Define struct type: type Name struct { fields }
- Create instance: var v = Name{...}
- Get pointer: p := &v
- Access fields via pointer: p.Field (no * needed)
- Modifying via pointer changes original struct
- Useful for efficient data handling
Full Transcript
This example shows how to use pointers to structs in Go. First, we define a struct type Person with Name and Age fields. Then, we create an instance p with Name Alice and Age 30. Next, we create a pointer pPtr that points to p. Using pPtr, we change the Age field to 31. Because pPtr points to p, this change updates p's Age as well. Finally, printing p.Age shows the updated value 31. This demonstrates how struct pointers let us modify the original data efficiently.