0
0
Goprogramming~10 mins

Value receivers in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Value receivers
Define struct
Define method with value receiver
Create instance of struct
Call method on instance
Method receives a COPY of instance
Modify copy inside method
Original instance remains unchanged
Method returns or prints result
Value receivers get a copy of the struct, so changes inside the method do not affect the original instance.
Execution Sample
Go
package main
import "fmt"
type Counter struct { count int }
func (c Counter) Increment() {
    c.count++
    fmt.Println("Inside method:", c.count)
}
func main() {
    c := Counter{count: 0}
    c.Increment()
    fmt.Println("After method call:", c.count)
}
This code shows a method with a value receiver that increments a count, but the original count stays the same.
Execution Table
StepVariableValue BeforeActionValue AfterOutput
1cCounter{count:0}Call Increment method (value receiver)Counter{count:0} (copy)
2c.count (copy)0Increment count inside method1Inside method: 1
3c (original)Counter{count:0}Method ends, copy discardedCounter{count:0}
4c.count (original)0Print count after method call0After method call: 0
💡 Method ends, copy of struct discarded, original struct unchanged
Variable Tracker
VariableStartAfter Step 2After Step 3Final
c (original)Counter{count:0}Counter{count:0}Counter{count:0}Counter{count:0}
c.count (copy inside method)N/A1N/AN/A
Key Moments - 2 Insights
Why does the original count not change after calling Increment?
Because the method uses a value receiver, it works on a copy of the struct, so changes inside the method do not affect the original variable (see execution_table step 3).
Is the 'c' inside the method the same as the 'c' in main?
No, inside the method 'c' is a copy of the original struct, so modifying it does not change the original (see variable_tracker showing separate values).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the value of c.count inside the method after increment?
A2
B0
C1
DUndefined
💡 Hint
Check the 'Value After' and 'Output' columns at step 2 in execution_table.
At which step does the original struct remain unchanged?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Value After' for 'c (original)' in execution_table step 3.
If the method used a pointer receiver instead, what would happen to c.count after the method call?
AIt would become 1
BIt would remain 0
CIt would become 2
DIt would cause an error
💡 Hint
Think about how pointer receivers allow methods to modify the original struct, unlike value receivers.
Concept Snapshot
Value receivers get a copy of the struct.
Methods with value receivers cannot change the original struct.
Changes inside the method affect only the copy.
Use value receivers for read-only methods.
Use pointer receivers to modify the original.
Full Transcript
In Go, methods can have value receivers, which means the method works on a copy of the struct. When you call a method with a value receiver, Go copies the struct and the method uses that copy. Any changes made inside the method affect only the copy, not the original struct. For example, if you have a Counter struct with a count field and a method Increment with a value receiver, calling Increment increases the count inside the copy, but the original count remains unchanged. This is shown in the execution table where the copy's count changes to 1 inside the method, but after the method ends, the original count is still 0. Beginners often get confused because they expect the original struct to change, but value receivers always work on copies. To modify the original struct, you need to use pointer receivers instead. This concept helps you decide when to use value or pointer receivers in your Go programs.