Course outline
Course / The terminal and the language

Build a greeting

Lab

How to fetch and submit this lab

Once: install the tool and connect your account.

curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon login

Each 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 submit

Go installed? gonomicon test runs the visible test on your computer first. Submitting also runs hidden checks on our server.

Goal

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 // TODO comment and a return line you will change.
  • Use both parameters: name decides who is greeted, excited decides the ending.
  • Use an if on the excited flag. An early return inside the if is 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

  1. The function has two inputs. The excited flag is a bool, so you can use it straight inside an if. Decide the name first, then the ending.
  2. If name is empty, swap it for "stranger" before building the text. Then if excited { ... } returns the version ending in !, and the final return gives the version ending in ..
  3. if name == "" { name = "stranger" }, then if excited { return "Hello, " + name + "!" }, then return "Hello, " + name + ".".

Something wrong with this lesson? Report a problem