0
0
Goprogramming~10 mins

Pointer receivers in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Pointer receivers
Define struct
Define method with pointer receiver
Create struct instance
Call method on instance
Method receives pointer
Modify struct fields
Changes visible outside method
This flow shows how a method with a pointer receiver modifies the original struct instance by receiving its memory address.
Execution Sample
Go
package main
import "fmt"
type Counter struct { count int }
func (c *Counter) Increment() { c.count++ }
func main() {
 c := Counter{}
 c.Increment()
 fmt.Println(c.count)
}
This code defines a struct with a pointer receiver method that increments a field, showing the change persists after the method call.
Execution Table
StepActionReceiver (c)Field c.countOutput
1Create Counter instance cCounter{count:0}0
2Call c.Increment()&c (pointer to c)0 before increment
3Inside Increment(), c.count++&c1 after increment
4Return from Increment()&c1
5Print c.countCounter{count:1}11
💡 Method call finished, c.count is updated to 1 and printed
Variable Tracker
VariableStartAfter Increment callFinal
c.count011
Key Moments - 2 Insights
Why does c.count change after calling Increment()?
Because Increment() uses a pointer receiver, it modifies the original struct's field directly, as shown in step 3 and 4 of the execution_table.
What if Increment() used a value receiver instead of a pointer?
Then Increment() would work on a copy of c, so changes to c.count inside the method would not affect the original, unlike the pointer receiver case in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of c.count immediately after step 3?
A0
B1
CUndefined
D2
💡 Hint
Check the 'Field c.count' column at step 3 in the execution_table.
At which step does the method receive a pointer to the struct?
AStep 1
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Receiver (c)' column to see when &c is passed.
If Increment() used a value receiver, what would be the output at step 5?
A0
B1
CCompilation error
DUndefined
💡 Hint
Recall that value receivers work on copies, so original c.count remains unchanged.
Concept Snapshot
Pointer receivers in Go methods:
- Use *Type to define pointer receiver
- Method receives address, can modify original struct
- Changes inside method affect caller's instance
- Use pointer receivers to avoid copying large structs
- Enables modifying struct fields directly
Full Transcript
This example shows a Go struct Counter with a method Increment that uses a pointer receiver. When main creates an instance c and calls c.Increment(), the method receives a pointer to c, allowing it to increase the count field directly. The execution table traces each step: creation of c with count 0, calling Increment which receives &c, incrementing count to 1, returning, and finally printing the updated count 1. Key points include that pointer receivers allow methods to modify the original struct, unlike value receivers which work on copies. The visual quiz tests understanding of when the pointer is received, the value changes, and the effect of using value receivers. This helps beginners see how pointer receivers enable methods to change struct data in Go.