0
0
Goprogramming~10 mins

Struct usage patterns in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Struct usage patterns
Define struct type
Create struct instance
Access or modify fields
Use struct in functions or methods
Pass struct by value or pointer
Update or read fields
End or reuse struct
This flow shows how to define a struct, create instances, access fields, and use structs in functions or methods, including passing by value or pointer.
Execution Sample
Go
package main

type Person struct {
    Name string
    Age  int
}

func main() {
    p := Person{Name: "Alice", Age: 30}
    p.Age = 31
}
Defines a Person struct, creates an instance, and updates the Age field.
Execution Table
StepActionCode LineVariable StateOutput/Result
1Define struct type Persontype Person struct {...}Person type createdNo output
2Create instance p with Name 'Alice' and Age 30p := Person{Name: "Alice", Age: 30}p = {Name: "Alice", Age: 30}No output
3Access and modify p.Age to 31p.Age = 31p = {Name: "Alice", Age: 31}No output
4End of main function}p = {Name: "Alice", Age: 31}Program ends
💡 Program ends after main function completes
Variable Tracker
VariableStartAfter Step 2After Step 3Final
pundefined{Name: "Alice", Age: 30}{Name: "Alice", Age: 31}{Name: "Alice", Age: 31}
Key Moments - 2 Insights
Why do we use pointers to structs instead of values sometimes?
Using pointers allows functions to modify the original struct fields, as shown in the flow where passing by pointer lets changes persist outside the function. Value passing copies the struct, so changes inside functions don't affect the original.
Can we access struct fields directly after creating an instance?
Yes, as shown in step 3 of the execution_table, you can access and modify fields directly using dot notation like p.Age.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of p after step 2?
A{Name: "Alice", Age: 30}
B{Name: "Alice", Age: 31}
Cundefined
Dnil
💡 Hint
Check the 'Variable State' column for step 2 in execution_table
At which step does the Age field of p change?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Variable State' columns in execution_table
If we passed p by value to a function and modified Age inside, what would happen?
AOriginal p.Age changes
BOriginal p.Age stays the same
CProgram crashes
DAge field is deleted
💡 Hint
Refer to key_moments about pointer vs value passing
Concept Snapshot
Struct usage patterns in Go:
- Define struct with type keyword
- Create instances with field names
- Access/modify fields with dot notation
- Pass structs by value or pointer
- Pointer passing allows modifying original
- Value passing copies struct, no original change
Full Transcript
This visual execution shows how to use structs in Go. First, we define a struct type named Person with fields Name and Age. Then, we create an instance p with Name 'Alice' and Age 30. Next, we access and modify the Age field to 31 using dot notation. The variable tracker shows how p changes from undefined to the new values. Key moments clarify why pointers are used to modify structs in functions and confirm direct field access. The quiz tests understanding of variable states and passing behavior. The snapshot summarizes the main points for quick review.