Course / The terminal and the language
Values that stay put
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-05-constants-that-never-change
cd 08-05-constants-that-never-change
gonomicon submitGoal
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.MaxRetriesandPiare currentlyvarlines with a// TODOcomment above each. MaxRetriesshould be a constant holding5, with no type written.Pishould be a constant of typefloat64holding3.14159.- Write them as two separate
constlines, or together in oneconst ( ... )block. Either works. - Do not change the two functions.
RetriesreturnsMaxRetries, andCirclereturnsPi * 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
- 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.
- MaxRetries is a plain whole number, so write it with no type at all. Pi should say float64 right after its name.
- const MaxRetries = 5 and const Pi float64 = 3.14159. You can write them on two separate lines, or together inside one const ( ... ) block.