Lesson 31 of 38 · Interfaces & Errors
Boss Battle: The Error Lich (Go Interfaces and Errors Challenge)
The Error Lich blocks the way. Every test you pass deals damage — pass them all to defeat it and claim a loot chest.
Deep beneath the Gopher Isles waits the Error Lich. It doesn't fight fair: it hides its failures in a maze of broken spells, hoping you'll lose track of just one. Parse your spellbook, cast every spell through a single interface, and report each failure with its exact line and cause — not one error may slip away.
Battle plan
This fight uses the whole chapter. Every spell implements one interface, so the casting loop never needs to know which spell it holds. A type switch turns spells back into text. Broken lines produce a custom error type that carries the line and the reason, running out of mana is a sentinel error, and every failure is wrapped with %w together with its line number — so errors.As and errors.Is can still dig out the cause.
A spellbook is a list of lines, one spell per line. Blank lines are allowed:
shield
fireball 10
heal 5
The starter declares the target, the Spell interface, the spell types and two sentinel errors:
type Target struct {
HP int
Shielded bool
}
type Spell interface {
Cost() int
Cast(t *Target) error
}
var (
ErrNoMana = errors.New("not enough mana")
ErrAlreadyShielded = errors.New("target is already shielded")
)
Assignment
1. Spells. Give Fireball, Heal and Shield both Spell methods, with value receivers:
| Spell | Cost() |
Cast(t) |
|---|---|---|
Fireball{Damage} |
3 |
If t.Shielded, the shield absorbs the blast: set Shielded to false and deal no damage. Otherwise subtract Damage from t.HP, never going below 0. |
Heal{Amount} |
2 |
Add Amount to t.HP. |
Shield{} |
1 |
Set t.Shielded to true. If the target is already shielded, change nothing and return ErrAlreadyShielded. |
In every other case, Cast returns nil.
2. (*ParseError).Error() returns cannot parse "<Line>": <Reason>, with the line formatted by %q, for example cannot parse "fireball ten": invalid amount.
3. ParseSpell(line string) (Spell, error) splits the line into words with strings.Fields, so extra spaces don't matter:
fireball NreturnsFireball{Damage: N},heal NreturnsHeal{Amount: N}andshieldreturnsShield{};- if the first word isn't exactly
fireball,healorshield— or there are no words at all — the reason isunknown spell; - a known spell with the wrong number of words (
fireball,heal 1 2,shield 3) has the reasonwrong number of arguments; - if
Nis not a whole number greater than 0 (ten,0,-4,2.5), the reason isinvalid amount.
On failure, return a nil spell and &ParseError{Line: line, Reason: reason} with the original, untrimmed line.
4. Describe(s Spell) string uses a type switch to turn a spell back into a line: fireball 10, heal 5 or shield — and unknown for anything else, including nil.
5. CastAll(lines []string, mana int, t *Target) (int, error) goes through the lines in order and returns how many spells it cast:
- lines are numbered from 1; blank lines (empty or only spaces) are skipped, but they still count;
- parse the line — on failure, return the error wrapped as
fmt.Errorf("line %d: %w", n, err); - if the spell costs more than the remaining
mana, returnErrNoMana, wrapped the same way; - otherwise subtract the cost from
manaand cast the spell ont; ifCastfails, wrap its error the same way; - stop at the first error and return it together with the number of spells cast successfully before it. If every line worked, the error is
nil.
Add "strconv" and "strings" to the imports.
t := &Target{HP: 30}
CastAll([]string{"shield", "fireball 10", "", "heal 5"}, 10, t)
// 3, nil — the shield absorbed the fireball, so t.HP is 35
CastAll([]string{"heal 5", "", "fireball ten"}, 10, t)
// 1, line 3: cannot parse "fireball ten": invalid amountPremium 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 .