QUICK START:HTMLCSSJavaScriptTypeScriptPythonSQLReactNext.jsNode.jsDSASystem DesignDevOpsCybersecurityAI / ML
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

go
go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package main
import "fmt"
type Developer struct {
Name string
Level int
}
// Pointer receiver mutates the struct
func (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 Code

Industry 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.