Coding/Learn/go/go structs and methods
Go Foundational Course
01.Introduction, Toolchain & Package main
02.Variables, Zero Values, Constants & iota
03.Control Flow: If with Init Statements & Switch
04.The Unified For Loop & Range Iteration
05.Functions, Multiple Return Values & Named Returns
06.Arrays vs Slices: Dynamic Memory & Headers
07.Maps: Key-Value Hash Tables & Safe Access
08.Pointers & Value vs Pointer Semantics
09.Structs, Receiver Methods & Composition
10.Implicit Interfaces & Duck Typing with Typesafety
11.Explicit Error Handling, Wrapping & Inspection
12.Defer, Panic & Recover: Resource Cleanup
13.Strings, Runes & UTF-8 Encoding Internals
14.Goroutines & The M:N Work-Stealing Scheduler
15.Channels: CSP Concurrency & Pipelines
16.Select Statement, Non-Blocking IO & Timeouts
17.Mutexes, RWMutex & Atomic Primitives
18.Context Propagation, Cancellation & Deadlines
19.JSON Serialization, Tags & net/http Routing
20.Capstone: High-Performance Concurrent Microservice Engine
Course Certificate0%

Complete all lessons to earn

LearnStructs, Receiver Methods & Composition
9 / 20 0%
Lesson 09 of 20
16 mins read

Structs, Receiver Methods & Composition

PreviousNext Lesson
In Plain English

Go achieves object-oriented design without classes. Group fields into structs, attach value and pointer receiver methods, and leverage struct embedding for elegant composition.

The Mental Model

"A struct is like an architectural blueprint. Methods are buttons on the device. Embedding is fitting a standard motor inside a blender instead of rebuilding the motor from scratch."

Deep Dive: How It Works

Struct Definition: Custom compound type aggregating named fields.

Receiver Methods: func (r ReceiverType) Method() attaches behaviors directly to types.

Composition over Inheritance: Struct embedding promotes inner fields and methods to the outer struct.

Syntax Blueprint

type User struct { Name string }
func (u *User) Rename(n string) { u.Name = n }

Declare struct fields and bind methods using value or pointer (*T) receivers.

PointCore Rules to Remember

Point star
Pointer vs Value Receivers: Use pointer receiver (*T) when mutating struct state; use value receiver for read-only.
Point star
Struct Embedding: Anonymous embedded structs promote inner methods to the parent struct automatically.
Point star
Constructor Idiom: Constructors are regular factory functions named NewType(...) returning *Type.

Common Beginner Traps & How to Fix Them

Mistake:Using value receiver func (a Account) Deposit(amount) and wondering why balance never changes.

Why it happens: A value receiver operates on a local copy of the struct.

How to fix: Change receiver to pointer receiver: func (a *Account) Deposit(amount).

Pro Tip: Be consistent across your type: if any method on a struct requires a pointer receiver, use pointer receivers for all methods on that struct.

Live Interactive Example

Hit Run Code to see it live
Bank Account with Pointer Receiver Methods
Go 1.22 / Runtime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Output Console
Click "Run Code" to view the rendered output.
How it works: Pointer receivers (*Account) allowed Deposit and Withdraw to update Balance directly.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Rectangle Area & Perimeter Methods

  • PointDefine a Rectangle struct with Width and Height float64 fields.
  • PointImplement an Area() float64 value receiver method.
  • PointImplement a Perimeter() float64 value receiver method.
  • PointIn main(), create a Rectangle with Width 5.0 and Height 3.0, and print "Area: %.1f, Perimeter: %.1f".
1
2
3
4
5
6
7
8
9
10
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates