Lesson 14 of 38 · Functions
Boss Battle: The Spellsmith (Go Functions Challenge)
The Spellsmith blocks the way. Every test you pass deals damage — pass them all to defeat it and claim a loot chest.
In a forge of black glass at the heart of the Gopher Isles works the Spellsmith, who hammers spells out of pure functions. He lets no one pass who cannot forge a spellbook of their own: spells that combine, spells that repeat, and a mana pool that never lets a caster overspend.
Battle plan
A spell is any function of type func(int) int: it takes the power of an attack and returns the new power. Because functions are values, spells can be stored in variables, passed to other functions and forged into new spells:
double := func(p int) int { return p * 2 }
addThree := func(p int) int { return p + 3 }
combo := Compose(addThree, double) // a brand-new spell
fmt.Println(combo(5)) // 16: (5 + 3) * 2
This battle draws on the whole chapter: function types and closures, variadic parameters and spreading, and multiple named results. Read every rule carefully — the edge cases are where the Spellsmith strikes back.
Assignment
Forge four functions.
1. Compose(spells ...func(int) int) func(int) int returns a new spell that feeds its input through every spell in order, from left to right: Compose(f, g)(x) is g(f(x)). With no spells, the returned spell returns its input unchanged. A composed spell can be cast any number of times and always gives the same result for the same input.
2. Repeat(spell func(int) int, times int) func(int) int returns a spell that casts spell times times in a row. If times is 0 or negative, the returned spell returns its input unchanged.
3. MakeManaPool(start int) (spend func(int) bool, remaining func() int) creates a pool holding start mana and returns two closures that share it:
spend(cost)takescostmana from the pool and returnstrue— but only ifcostis not negative and not larger than the mana left (a cost of0always succeeds). Otherwise it returnsfalseand leaves the pool unchanged.remaining()returns the mana left in the pool.
Every call of MakeManaPool creates a separate pool.
4. Stats(nums ...int) (count, total int, avg float64) returns how many numbers it received, their sum and their average. With no numbers, all three results are 0.
Compose(addThree, double)(5) // 16: (5 + 3) * 2
Compose(double, addThree)(5) // 13: 5 * 2 + 3
Compose()(42) // 42
Repeat(double, 3)(1) // 8
Repeat(addThree, 0)(7) // 7
spend, remaining := MakeManaPool(10)
spend(3) // true: 7 left
spend(8) // false: still 7 left
remaining() // 7
Stats(4, 8, 6, 3) // 4, 21, 5.25
Stats() // 0, 0, 0Premium 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 .