0
0
Goprogramming~10 mins

Defining methods in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Defining methods
Define struct type
Define method with receiver
Create instance of struct
Call method on instance
Method runs using instance data
Return or print result
End
This flow shows how to define a method on a struct type, create an instance, and call the method to use the instance's data.
Execution Sample
Go
package main
import "fmt"
type Person struct { name string }
func (p Person) Greet() string { return "Hello, " + p.name }
func main() {
 p := Person{name: "Anna"}
 fmt.Println(p.Greet())
}
This code defines a method Greet on Person struct and calls it to print a greeting.
Execution Table
StepActionEvaluationResult
1Define struct Person with field nameNo outputPerson type ready
2Define method Greet with receiver p PersonNo outputMethod Greet linked to Person
3Create instance p = Person{name: "Anna"}p.name = "Anna"Instance p created
4Call p.Greet()Concatenate "Hello, " + p.name"Hello, Anna"
5Print result of p.Greet()Output to consoleHello, Anna
💡 Program ends after printing greeting
Variable Tracker
VariableStartAfter Step 3After Step 4Final
pundefinedPerson{name: "Anna"}Person{name: "Anna"}Person{name: "Anna"}
p.nameundefined"Anna""Anna""Anna"
Key Moments - 3 Insights
Why do we write (p Person) before the method name?
This part defines the method receiver, meaning the method works on a Person instance named p. See execution_table step 2 where method links to Person.
Does the method change the original Person instance?
No, because the receiver is a value copy (not a pointer). The method uses a copy of p. See variable_tracker where p stays the same after method call.
How does the method access the name field?
Inside the method, p.name accesses the name of the Person instance passed as receiver. See execution_table step 4 where p.name is used.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of p.name used in the method?
A"Person"
B"Hello"
C"Anna"
D"Greet"
💡 Hint
Check the 'Evaluation' column at step 4 in execution_table where p.name is concatenated.
At which step is the method Greet linked to the Person type?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table step 2 where method definition happens.
If we changed the receiver to a pointer like (p *Person), what would happen to the variable p after calling Greet?
Ap could be modified inside the method
Bp would be unchanged
Cp would be nil
Dp would become a copy
💡 Hint
Refer to key_moments about receiver type and variable_tracker showing p unchanged with value receiver.
Concept Snapshot
Defining methods in Go:
- Use func (receiver Type) MethodName() syntax
- Receiver links method to struct type
- Methods can access receiver fields
- Value receivers get a copy; pointer receivers can modify original
- Call methods on instances with instance.MethodName()
Full Transcript
This example shows how to define a method on a struct in Go. First, we define a struct Person with a name field. Then, we define a method Greet with a receiver p of type Person. This method returns a greeting string using p.name. In main, we create an instance p with name "Anna". Calling p.Greet() runs the method using p's data and returns "Hello, Anna". The method uses a value receiver, so it works on a copy of p and does not modify the original. The program prints the greeting and ends.