Course outline
Course / The terminal and the language

Values that stay put

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-05-constants-that-never-change
cd 08-05-constants-that-never-change
gonomicon submit

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

Goal

Turn two ordinary variables into constants: MaxRetries (an untyped whole number) and Pi (a typed float64). Two small functions already use them.

Example

If a program needed a user limit and a gravity value, you would write:

const MaxUsers = 100
const Gravity float64 = 9.81

MaxUsers has no type, so Go lets it adapt to wherever it is used. Gravity is locked to float64.

Rules

  • Edit constants.go. MaxRetries and Pi are currently var lines with a // TODO comment above each.
  • MaxRetries should be a constant holding 5, with no type written.
  • Pi should be a constant of type float64 holding 3.14159.
  • Write them as two separate const lines, or together in one const ( ... ) block. Either works.
  • Do not change the two functions. Retries returns MaxRetries, and Circle returns Pi * 2.
  • Do not type the numbers into the functions. They must keep using the constants.
  • Do not add imports. You will not need any.

What you'll practice

Declaring constants with const, an untyped constant versus a typed one, and the difference between a var and a const.

Run it

gonomicon fetch week-1/08-05-constants-that-never-change
cd 08-05-constants-that-never-change
gonomicon test
gonomicon submit

Hints

  1. Right now MaxRetries and Pi are ordinary variables holding 0. Constants are declared with the word const instead of var, and their value can never change.
  2. MaxRetries is a plain whole number, so write it with no type at all. Pi should say float64 right after its name.
  3. const MaxRetries = 5 and const Pi float64 = 3.14159. You can write them on two separate lines, or together inside one const ( ... ) block.

Something wrong with this lesson? Report a problem