0
0
Goprogramming~10 mins

Method call behavior in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Method call behavior
Create object
Call method on object
Method receives object or pointer
Method executes code
Return result or modify object
Continue program
This flow shows how a method is called on an object, how the method receives the object or pointer, executes, and returns or modifies the object.
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 creates a Counter object, calls its Increment method which increases count by 1, then prints the count.
Execution Table
StepActionObject State (count)Method ReceiverOutput
1Create Counter ccount=0N/AN/A
2Call c.Increment()count=0 before call*Counter (pointer to c)N/A
3Inside Increment methodcount=1 after increment*CounterN/A
4Return from Incrementcount=1N/AN/A
5Print c.countcount=1N/A1
💡 Program ends after printing count value 1
Variable Tracker
VariableStartAfter Step 2After Step 3Final
c.count0011
Key Moments - 2 Insights
Why does the count change after calling Increment even though we pass a pointer?
Because the method receiver is a pointer (*Counter), the method modifies the original object, so count changes as shown in execution_table step 3.
What if the method receiver was not a pointer, would count change?
If the receiver was a value (Counter), the method would work on a copy, so changes inside the method would not affect the original count (not shown here).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of c.count immediately after step 2?
AUndefined
B1
C0
D2
💡 Hint
Check the 'Object State (count)' column at step 2 in the execution_table.
At which step does the count actually increase?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Object State (count)' column and see when count changes from 0 to 1.
If the Increment method had a value receiver instead of pointer, what would happen to c.count after calling Increment?
AIt would remain 0
BIt would increase to 1
CIt would cause a compile error
DIt would become -1
💡 Hint
Recall key_moments explanation about pointer vs value receivers affecting original object.
Concept Snapshot
Method call behavior in Go:
- Methods can have value or pointer receivers.
- Pointer receivers (*Type) modify original object.
- Value receivers (Type) work on copies.
- Call syntax: obj.Method()
- Changes in pointer receiver methods affect original object.
Full Transcript
This example shows how a method call works in Go. We create a Counter object with count 0. When we call the Increment method with a pointer receiver, it increases count by 1 inside the method. The change affects the original object because the method receives a pointer. Finally, printing c.count outputs 1. If the method used a value receiver, the count would not change outside the method. This helps understand how methods can modify objects in Go.