0
0
Goprogramming~10 mins

Defining structs in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Defining structs
Start
Define struct type
Create struct variable
Assign values to fields
Use struct variable
End
This flow shows how you define a struct type, create a variable of that type, assign values to its fields, and then use it.
Execution Sample
Go
package main

type Person struct {
    Name string
    Age  int
}

func main() {
    var p Person
    p.Name = "Alice"
    p.Age = 30
}
Defines a struct Person with fields Name and Age, creates a variable p, and assigns values to its fields.
Execution Table
StepActionEvaluationResult
1Define struct type PersonPerson struct with fields Name(string), Age(int)Type Person created
2Declare variable p of type Personp is zero-valued Personp.Name = "", p.Age = 0
3Assign p.Name = "Alice"Set field Namep.Name = "Alice"
4Assign p.Age = 30Set field Agep.Age = 30
5Use p (e.g., print)Access fieldsOutputs: Name: Alice, Age: 30
💡 All steps completed, struct variable p fully initialized and used
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
p.Name"""""Alice""Alice""Alice"
p.Age0003030
Key Moments - 3 Insights
Why does p.Name start as an empty string before assignment?
In step 2 of the execution_table, p is declared but not assigned, so Go sets string fields to their zero value, which is an empty string.
Can I assign values to struct fields before declaring the variable?
No, as shown in step 3 and 4, you must declare the variable (step 2) before assigning values to its fields.
What happens if I forget to assign a field value?
That field keeps its zero value, like p.Age would stay 0 if not assigned, as seen in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the variable_tracker, what is the value of p.Age after Step 3?
A30
B0
C"Alice"
Dundefined
💡 Hint
Check the 'After Step 3' column for p.Age in variable_tracker
At which step in execution_table is the struct variable p declared?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for 'Declare variable p' action in execution_table
If you skip assigning p.Name, what would be its final value according to variable_tracker?
A"" (empty string)
B"Alice"
Cnil
D0
💡 Hint
See the 'Start' and 'After Step 2' values for p.Name in variable_tracker
Concept Snapshot
Defining structs in Go:
- Use 'type Name struct { fields }' to define
- Declare variable: 'var v Name'
- Fields have zero values initially
- Assign fields: 'v.Field = value'
- Use struct variable after assignment
Full Transcript
This example shows how to define a struct type named Person with two fields: Name and Age. First, the struct type is created. Then, a variable p of type Person is declared, which initializes its fields to zero values (empty string for Name, 0 for Age). Next, values are assigned to the fields: p.Name is set to "Alice" and p.Age to 30. Finally, the struct variable p can be used, for example, printed to show its field values. The variable tracker shows how p.Name and p.Age change step by step. Key moments clarify why fields start with zero values and the order of declaration and assignment. The quiz tests understanding of variable values at different steps and the effect of skipping assignments.