Course / The terminal and the language
Count with iota
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/08-06-iota-weekdays
cd 08-06-iota-weekdays
gonomicon submitGoal
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,MediumandHighare currently threevarlines with a// TODOcomment. - Delete those three
varlines and write oneconst ( ... )block in their place. - The names must come in this order:
Low,Medium,High. - Use
iotaon the first line only. Do not type the numbers 0, 1 or 2 yourself. - Do not change the
Levelfunction. It returnsHigh. - 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
- 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.
- 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.
- const ( Low = iota Medium High ). Leave the Level function alone: it already returns High.