Intermediate 18 min readModule: Module 6: Structs, Methods & Pointer Receivers
Structs, Composition & Value vs Pointer Receivers
Define structured data types, embed structs for composition, and mutate state with pointer receivers (*T).
What You Will Learn in This Lesson
- Defining struct data structures
- Value receivers func (t T) vs Pointer receivers func (t *T)
- Struct embedding (composition) instead of classical OOP inheritance
Introduction & Core Concept
Go does not have classes or inheritance. Instead, it uses Structs with Methods, and achieves code reuse through Struct Embedding (Composition).
WHY DOES THIS MATTER IN THE REAL WORLD?
Using a value receiver creates a copy of the struct; a pointer receiver (*T) mutates the original struct in place.
Struct with Pointer Receiver Method
gogo
123456789101112131415161718package mainimport "fmt"type Developer struct {Name stringLevel int}// Pointer receiver mutates the structfunc (d *Developer) Promote() {d.Level++}func main() {dev := Developer{Name: "Alex", Level: 1}dev.Promote()fmt.Printf("%s is now Level %d\n", dev.Name, dev.Level)}
Line-by-Line Technical Breakdown
1Embedded structs promote their fields to the outer struct automatically.
Try It Yourself (Interactive Editor)
Modify the code in real-time and click Run to test live browser output and console logs.
Intelligent Code Runner & Live Sandbox[GO]
GO SOURCE EDITOR
Interactive Live CodeIndustry Best Practices & Professional Standards
- Use pointer receivers whenever a method needs to mutate the struct or if the struct is large.
Lesson Summary & Core Takeaways
- Structs and pointer receivers provide clean, explicit data mutation.