Course outline
Course / The terminal and the language

Count with iota

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/08-06-iota-weekdays
cd 08-06-iota-weekdays
gonomicon submit

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

Goal

Replace three hand-numbered variables with a const block that uses iota, so the names Low, Medium and High get the numbers 0, 1 and 2 automatically.

Example

If you needed three sizes, you would write:

const (
	Small = iota
	Large
	Huge
)

Small is 0, Large is 1, and Huge is 2. Only the first line says iota. The other lines repeat the same rule, and the counter goes up by one each line.

Rules

  • Edit levels.go. Low, Medium and High are currently three var lines with a // TODO comment.
  • Delete those three var lines and write one const ( ... ) block in their place.
  • The names must come in this order: Low, Medium, High.
  • Use iota on the first line only. Do not type the numbers 0, 1 or 2 yourself.
  • Do not change the Level function. It returns High.
  • Do not add imports. You will not need any.

What you'll practice

A const block, iota counting from zero, and why a name with no value repeats the line above it.

Run it

gonomicon fetch week-1/08-06-iota-weekdays
cd 08-06-iota-weekdays
gonomicon test
gonomicon submit

Hints

  1. Right now Low, Medium and High are three separate var lines holding 0. Replace them with one const ( ... ) block, so the compiler can number the names for you.
  2. In a const block, write Low = iota on the first line. The next names need no value: a line with no value repeats the line above it, and iota counts up by one each line.
  3. const ( Low = iota Medium High ). Leave the Level function alone: it already returns High.

Something wrong with this lesson? Report a problem