Course / The terminal and the language
Build a greeting
How to fetch and submit this lab
Once: install the tool and connect your account.
curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon loginEach lab: download it, edit the file named in the instructions, then submit.
gonomicon fetch week-1/12-02-greeting-builder
cd 12-02-greeting-builder
gonomicon submitGoal
Fill in Greet so it returns "Hello, NAME.", or "Hello, NAME!" when excited is true, and greets "stranger" when the name is empty.
Example
fmt.Println(Greet("Ana", false)) // Hello, Ana.
fmt.Println(Greet("Ana", true)) // Hello, Ana!
fmt.Println(Greet("", false)) // Hello, stranger.
fmt.Println(Greet("", true)) // Hello, stranger!
The name is copied into the greeting exactly as given, so letters like é or 李 stay intact.
Rules
- Edit
greet.go. The function has a// TODOcomment and areturnline you will change. - Use both parameters:
namedecides who is greeted,exciteddecides the ending. - Use an
ifon theexcitedflag. An earlyreturninside theifis a good fit. - The text is exactly
Hello,, then the name, then.or!. No extra spaces, no extra characters. - Do not change the line
func Greet(name string, excited bool) string {. Do not add imports.
What you'll practice
Using two parameters of different types, if with a bool, early returns, and building a string to return.
Run it
gonomicon fetch week-1/12-02-greeting-builder
cd 12-02-greeting-builder
gonomicon test
gonomicon submit
Hints
- The function has two inputs. The
excitedflag is abool, so you can use it straight inside anif. Decide the name first, then the ending. - If
nameis empty, swap it for"stranger"before building the text. Thenif excited { ... }returns the version ending in!, and the finalreturngives the version ending in.. if name == "" { name = "stranger" }, thenif excited { return "Hello, " + name + "!" }, thenreturn "Hello, " + name + ".".