Bird
Raised Fist0
Goprogramming~10 mins

Struct pointers in Go - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. What does a struct pointer in Go hold?
type Person struct { Name string }
easy
A. Only the struct's first field value
B. A copy of the struct data
C. The memory address of a struct instance
D. A string representation of the struct

Solution

  1. Step 1: Understand what a pointer stores

    A pointer stores the memory address of a variable, not the variable's value itself.
  2. Step 2: Apply to struct pointers

    A struct pointer holds the address of the struct instance, allowing access or modification of the original struct.
  3. Final Answer:

    The memory address of a struct instance -> Option C
  4. Quick Check:

    Struct pointer = memory address [OK]
Hint: Pointers hold addresses, not copies. Remember & for address [OK]
Common Mistakes:
  • Thinking pointer holds a copy of the struct
  • Confusing pointer with struct value
  • Assuming pointer holds only first field
2. Which of the following is the correct way to declare a pointer to a struct Person in Go?
easy
A. var *p Person
B. var p Person*
C. var p &Person
D. var p *Person

Solution

  1. Step 1: Recall Go pointer syntax

    In Go, a pointer type is declared by placing * before the type name, e.g., *Person.
  2. Step 2: Match correct declaration

    var p *Person uses var p *Person, which is the correct syntax for a pointer variable.
  3. Final Answer:

    var p *Person -> Option D
  4. Quick Check:

    Pointer declaration = *Type [OK]
Hint: Pointer type syntax: *TypeName, not after variable [OK]
Common Mistakes:
  • Placing * after variable name
  • Using & in declaration instead of *
  • Incorrect order of * and type
3. What is the output of this Go program?
package main
import "fmt"
type Point struct { X, Y int }
func main() {
 p := Point{1, 2}
 ptr := &p
 ptr.X = 10
 fmt.Println(p.X)
}
medium
A. 1
B. 10
C. 0
D. Compilation error

Solution

  1. Step 1: Understand pointer modification

    ptr points to p. Changing ptr.X modifies p.X because ptr references p's memory.
  2. Step 2: Check printed value

    After setting ptr.X = 10, p.X becomes 10. So, printing p.X outputs 10.
  3. Final Answer:

    10 -> Option B
  4. Quick Check:

    Pointer changes original struct field [OK]
Hint: Changing pointer field changes original struct field [OK]
Common Mistakes:
  • Thinking ptr.X is separate copy
  • Expecting original p.X to remain 1
  • Confusing pointer dereference syntax
4. Identify the error in this Go code snippet:
package main
import "fmt"
type User struct { Name string }
func updateName(u User) {
 u.Name = "Alice"
}
func main() {
 user := User{"Bob"}
 updateName(&user)
 fmt.Println(user.Name)
}
medium
A. Passing pointer to function expecting value
B. Missing import for fmt package
C. Cannot assign string to Name field
D. Function parameter should be a pointer to User

Solution

  1. Step 1: Check function parameter type

    Function updateName expects a User value, but main passes &user (a *User pointer).
  2. Step 2: Identify the type mismatch

    This causes a compilation error because the argument type (*User) does not match the parameter type (User).
  3. Final Answer:

    Passing pointer to function expecting value -> Option A
  4. Quick Check:

    Function param must match argument type [OK]
Hint: Match function param type with argument (& for pointer) [OK]
Common Mistakes:
  • Passing pointer to value parameter without pointer receiver
  • Ignoring need to modify original struct
  • Assuming string assignment causes error
5. Given this struct and function:
package main
import "fmt"
type Counter struct { Count int }
func increment(c *Counter) {
 c.Count++
}
func main() {
 c := Counter{Count: 5}
 increment(&c)
 increment(c)
 fmt.Println(c.Count)
}

What will happen when running this program?
hard
A. Compilation error at increment(c)
B. Output is 7
C. Output is 6
D. Runtime panic due to invalid pointer

Solution

  1. Step 1: Analyze function calls

    increment expects a *Counter pointer. The first call increment(&c) is correct.
  2. Step 2: Check second call increment(c)

    Passing c (a value) instead of *Counter causes a compilation error because types don't match.
  3. Final Answer:

    Compilation error at increment(c) -> Option A
  4. Quick Check:

    Pointer param requires pointer argument [OK]
Hint: Pointer params need pointer args; value won't auto-convert [OK]
Common Mistakes:
  • Assuming Go auto-converts value to pointer
  • Expecting output 7 without error
  • Ignoring type mismatch in function call
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard