Lesson 9 of 38 · Fundamentals
Boss Battle: The Gatekeeper (Go Fundamentals Challenge)
The Gatekeeper blocks the way. Every test you pass deals damage — pass them all to defeat it and claim a loot chest.
At the far end of the first island, a stone bridge leads on to the rest of the Gopher Isles — and in front of it sits The Gatekeeper, an ancient stone gopher with glowing eyes. Nobody crosses until they answer its three riddles, and the Gatekeeper checks every answer with merciless precision.
This battle combines everything from the Fundamentals chapter: variables and arithmetic with %, strings and formatting, decisions with if and switch, and for loops over numbers and runes.
Battle plan
- Building a string in a loop: start with an empty string and append with
+=. To separate words with single spaces, add the space before every word except the first. - Numbers to text:
strconv.Itoa(7)returns"7"(import"strconv");fmt.Sprint(7)works too. - Divisibility:
i%3 == 0meansiis a multiple of 3. In aswitch, the first matching case wins, so check the most specific case first. - Classifying runes: rune literals compare like numbers, so
r >= 'a' && r <= 'z'is true exactly for the lower-case lettersatoz. The same trick works for'A'–'Z'and'0'–'9'. - Letter positions: because runes are numbers,
r - 'a'is0for'a',1for'b'and so on. Convert it withint(...)before adding it to anint. - Counting characters:
lencounts bytes. To count characters, count the iterations offor _, r := range s.
r := 'c'
fmt.Println(r >= 'a' && r <= 'z') // true
fmt.Println(int(r-'a') + 1) // 3
Assignment
Write the three functions whose skeletons are given. Each one answers a riddle.
Riddle 1: the chant
Chant(n int) string returns the numbers from 1 to n separated by single spaces, except that multiples of 3 are replaced by "Go", multiples of 5 by "pher", and multiples of both 3 and 5 by "Gopher". There are no leading or trailing spaces, and if n is less than 1 the result is the empty string "".
Chant(5) // "1 2 Go 4 pher"
Chant(15) // "1 2 Go 4 pher Go 7 8 Go pher 11 Go 13 14 Gopher"
Riddle 2: the password
PasswordStrength(pw string) string rates a password as "weak", "medium" or "strong":
- The length of a password is its number of characters (runes), not bytes.
- There are three kinds of characters: upper-case letters
A–Z, lower-case lettersa–zand digits0–9. Anything else — spaces, symbols, letters likeé— counts toward the length but is not a kind. "strong": at least 12 characters and all three kinds."medium": not strong, but at least 8 characters and at least two kinds."weak": everything else.
PasswordStrength("password") // "weak": only one kind
PasswordStrength("Passw0rd") // "medium": three kinds, but only 8 characters
PasswordStrength("CorrectHorse42") // "strong"
Riddle 3: the rune score
RuneScore(word string) int adds up the alphabet positions of the letters in word: a and A are worth 1, b and B are worth 2, and so on up to 26 for z and Z. Every other character is worth 0.
RuneScore("abc") // 6
RuneScore("Go, Go!") // 44
Each passing test cracks the Gatekeeper's stone armour. Pass all ten tests to cross the bridge!
Keep learning
Need a quick reminder? The Go cheat sheet has every topic of Learn Go on one page.
Lesson last updated on .