Course outline
Course / The terminal and the language

Two ways to make a variable

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-01-declare-with-var-and-short
cd 08-01-declare-with-var-and-short
gonomicon submit

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

Goal

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 // TODO comment and a return line you will change.
  • In Greeting, declare a variable with var, give it the type string and the value "Hello", and return it.
  • In Visits, declare a variable with := holding the whole number 3, and return it.
  • The function names and what they return are fixed. Do not change the lines func Greeting() string { and func 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

  1. You need one box made with var and one made with :=. Each function makes its box, then returns it.
  2. var greeting string = "Hello" spells out the type. visits := 3 lets Go work out the type. Replace the return line in each function so it returns your variable.
  3. Greeting: var greeting string = "Hello" then return greeting. Visits: visits := 3 then return visits.

Something wrong with this lesson? Report a problem