Course outline
Course / The terminal and the language

Name the day

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/10-04-name-the-day
cd 10-04-name-the-day
gonomicon submit

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

Goal Turn a package-level Day number into its name and its kind, using a switch in each function.

Example If you needed to name a month number instead, you'd write:

func MonthName() string {
	switch Month {
	case 1:
		return "January"
	case 2:
		return "February"
	default:
		return "invalid"
	}
}

Rules

  • Edit day.go. Both DayName and DayKind have a // TODO comment and a return line you will change.
  • Write both with a switch on Day, not an if / else if chain.
  • DayName: 1 is "Monday" through 7 is "Sunday". Anything else is "invalid".
  • DayKind: 1 through 5 are "weekday". 6 and 7 are "weekend". Anything else is "invalid". List 6 and 7 together on one case line, the same way the lesson lists several values on one line.
  • Do not change var Day int or either function's signature. Do not add imports.

What you'll practice Writing a switch instead of a long if / else if chain, and listing several matching values on one case line.

Run it

gonomicon fetch week-1/10-04-name-the-day
cd 10-04-name-the-day
gonomicon test
gonomicon submit

Hints

  1. Use a switch on Day for both functions instead of an if/else-if chain, with a default case for anything invalid.
  2. DayKind doesn't need five separate weekday cases: list 1, 2, 3, 4, 5 together on one case line, and 6, 7 together on another.
  3. switch Day { case 1: return "Monday" ... case 7: return "Sunday" default: return "invalid" } and switch Day { case 1, 2, 3, 4, 5: return "weekday" case 6, 7: return "weekend" default: return "invalid" }

Something wrong with this lesson? Report a problem