Course / The terminal and the language
Two ways to make a variable
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-01-declare-with-var-and-short
cd 08-01-declare-with-var-and-short
gonomicon submitGoal
Fill in two small functions. Greeting makes a box with var and returns it. Visits makes a box with := and returns it.
Example
If a function were meant to return the city "Lisbon", you would write:
func City() string {
var city string = "Lisbon"
return city
}
The variable is declared first, and the return line hands back what is in the box.
Rules
- Edit
declare.go. Each function has a// TODOcomment and areturnline you will change. - In
Greeting, declare a variable withvar, give it the typestringand the value"Hello", and return it. - In
Visits, declare a variable with:=holding the whole number3, and return it. - The function names and what they return are fixed. Do not change the lines
func Greeting() string {andfunc Visits() int {. - Do not add imports. You will not need any.
- Remember that
:=only works inside a function, which is exactly where you are.
What you'll practice
Declaring a variable with var, declaring one with :=, and returning it.
Run it
gonomicon fetch week-1/08-01-declare-with-var-and-short
cd 08-01-declare-with-var-and-short
gonomicon test
gonomicon submit
Hints
- You need one box made with
varand one made with:=. Each function makes its box, then returns it. var greeting string = "Hello"spells out the type.visits := 3lets Go work out the type. Replace thereturnline in each function so it returns your variable.- Greeting:
var greeting string = "Hello"thenreturn greeting. Visits:visits := 3thenreturn visits.