0
0
Goprogramming~10 mins

Receiver types in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Receiver types
Define struct type
Define method with receiver
Create instance of struct
Call method on instance
Method uses receiver to access/modify data
Observe effect on instance
This flow shows how methods with receiver types are defined on structs and called on instances to access or modify data.
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 Counter with a pointer receiver method Increment that increases count by 1, then prints the updated count.
Execution Table
StepActionReceiver TypeReceiver ValueEffect on countOutput
1Define struct Counter----
2Define method Increment with pointer receiver*Counter---
3Create instance c of CounterCountercount=0count=0-
4Call c.Increment()*Countercount=1count incremented to 1-
5Print c.count---1
💡 Program ends after printing count value 1
Variable Tracker
VariableStartAfter IncrementFinal
c.count011
Key Moments - 2 Insights
Why does the Increment method use a pointer receiver (*Counter) instead of a value receiver (Counter)?
Using a pointer receiver allows the method to modify the original struct's data. As shown in step 4 of the execution_table, the count is incremented on the original instance. If a value receiver was used, the method would modify a copy, and the original count would stay 0.
What happens if we call Increment on a value receiver instead of a pointer receiver?
The method would receive a copy of the struct, so changes inside the method won't affect the original instance. The count would remain unchanged after calling Increment, unlike in step 4 where the pointer receiver modifies the original.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of c.count after step 4?
A1
B0
CUndefined
D2
💡 Hint
Check the 'Effect on count' column in step 4 of the execution_table.
At which step is the method Increment called with a pointer receiver?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' and 'Receiver Type' columns in the execution_table.
If the Increment method used a value receiver instead of a pointer, what would be the output at step 5?
A1
B0
CCompilation error
D2
💡 Hint
Refer to the key_moments explanation about pointer vs value receivers.
Concept Snapshot
Receiver types in Go:
- Methods can have receivers: value (T) or pointer (*T).
- Pointer receivers allow methods to modify the original struct.
- Value receivers get a copy; changes don't affect original.
- Use pointer receivers to change struct data inside methods.
- Call methods on instances; Go handles pointer/value automatically.
Full Transcript
This example shows how Go methods use receiver types to access or modify struct data. We define a struct Counter with a field count. The method Increment has a pointer receiver *Counter, so it can increase the count field of the original struct. We create an instance c with count 0, call c.Increment(), which changes count to 1, then print the count. Using a pointer receiver is important because it allows the method to modify the original data, not just a copy. The execution table traces each step, showing how the count changes. The variable tracker highlights the count value before and after Increment. Key moments clarify why pointer receivers are needed and what happens if a value receiver is used. The quiz tests understanding of these points. This helps beginners see how receiver types affect method behavior in Go.