Skip to content
Codewilds

Lesson 25 of 38 · Structs & Methods

Boss Battle: The Clockwork Golem (Go Structs and Methods Challenge)

Boss battlehard100 XP2 min read

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 Machine holding a []Part.
  • Embedding: Golem embeds Machine, so g.Parts, g.Find(...) and g.BrokenParts() work directly on a golem.
  • Pointer receivers: Hit and Repair change 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. A for _, p := range loop 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:

  1. NewGolem(name string, energy int, parts ...Part) *Golem returns a pointer to a golem with that name, energy and the parts in the given order.
  2. (m *Machine) Find(name string) *Part returns a pointer to the part with that name inside m.Parts (changes made through it must change the machine), or nil if there is no such part.
  3. (m *Machine) BrokenParts() []string returns the names of all broken parts, sorted alphabetically (empty when nothing is broken).
  4. (g *Golem) Hit(damage int): if damage is 0 or negative, nothing happens. Otherwise the first working part (in slice order) loses damage durability, but never drops below 0. Leftover damage is lost — it doesn't spill over to the next part. Every such hit also drains 1 Energy, which never drops below 0, even when every part is already broken.
  5. (g *Golem) Repair(name string, amount int) bool adds amount to the named part's durability, but never above MaxDurability, and returns true. Broken parts can be repaired too. If the part doesn't exist or amount is 0 or negative, it returns false and changes nothing.
  6. (g *Golem) Operational() bool reports whether the golem has Energy above 0, at least one part, and no broken parts.
  7. (g *Golem) Status() string returns "<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
Play the 9 free lessons

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 .

Write code ↓
Go+100 XP

Lesson 25 is part of the full course. The first 9 lessons are free.

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
Play the 9 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.