Lesson 25 of 38 · Structs & Methods
Boss Battle: The Clockwork Golem (Go Structs and Methods Challenge)
The Clockwork Golem blocks the way. Every test you pass deals damage — pass them all to defeat it and claim a loot chest.
The bridge to the third island is guarded by the Clockwork Golem, a towering machine of brass gears and hissing steam that creaks louder with every step you take. Model it in Go — every part, every hit, every repair — and you decide whether it gets patched up or taken apart, gear by gear.
Battle plan
This boss combines everything from the chapter:
- Structs and slices of structs: the golem's body is a
Machineholding a[]Part. - Embedding:
GolemembedsMachine, sog.Parts,g.Find(...)andg.BrokenParts()work directly on a golem. - Pointer receivers:
HitandRepairchange the golem, so — following the consistency rule — every method uses a pointer receiver. - Pointers into slices:
&m.Parts[i]points at the element inside the slice. Afor _, p := rangeloop hands you a copy, and a pointer to that copy changes nothing.
A part is working while its Durability is above 0, and broken when it is 0 (or less).
Assignment
The starter declares a constant and three types:
const MaxDurability = 100
type Part struct {
Name string
Durability int
}
type Machine struct {
Parts []Part
}
type Golem struct {
Machine
Name string
Energy int
}
Implement:
NewGolem(name string, energy int, parts ...Part) *Golemreturns a pointer to a golem with that name, energy and the parts in the given order.(m *Machine) Find(name string) *Partreturns a pointer to the part with that name insidem.Parts(changes made through it must change the machine), ornilif there is no such part.(m *Machine) BrokenParts() []stringreturns the names of all broken parts, sorted alphabetically (empty when nothing is broken).(g *Golem) Hit(damage int): ifdamageis 0 or negative, nothing happens. Otherwise the first working part (in slice order) losesdamagedurability, but never drops below 0. Leftover damage is lost — it doesn't spill over to the next part. Every such hit also drains 1Energy, which never drops below 0, even when every part is already broken.(g *Golem) Repair(name string, amount int) booladdsamountto the named part's durability, but never aboveMaxDurability, and returnstrue. Broken parts can be repaired too. If the part doesn't exist oramountis 0 or negative, it returnsfalseand changes nothing.(g *Golem) Operational() boolreports whether the golem hasEnergyabove 0, at least one part, and no broken parts.(g *Golem) Status() stringreturns"<Name>: <working>/<total> parts working, energy <Energy>". If any parts are broken, it appends" (broken: <names>)"with the sorted broken names separated by", ".
g := NewGolem("Brassheart", 3,
Part{Name: "Head", Durability: 20},
Part{Name: "Arm", Durability: 30},
Part{Name: "Leg", Durability: 40},
)
g.Hit(25) // Head 20 → 0 (5 damage is lost), energy 2
g.Hit(10) // Arm 30 → 20, energy 1
fmt.Println(g.Status()) // Brassheart: 2/3 parts working, energy 1 (broken: Head)
fmt.Println(g.Operational()) // false
g.Repair("Head", 15)
fmt.Println(g.Status()) // Brassheart: 3/3 parts working, energy 1
fmt.Println(g.Operational()) // true
You'll need the sort (or slices) and strings packages, so add them to the imports.
Premium lesson
Unlock the full Learn Go course
- All 38 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Keep learning
Need a quick reminder? The Go cheat sheet has every topic of Learn Go on one page.
Lesson last updated on .