Course outline
Course / The terminal and the language

What a box holds before you fill it

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-02-zero-values-by-default
cd 08-02-zero-values-by-default
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 four small functions. Each one makes a box with var but never puts anything in it, then returns the box. Go supplies the zero value for you.

Example

If a function were meant to return an empty uint box, you would write:

func ZeroCount() uint {
	var count uint
	return count
}

There is no = and no value. The box is created, Go fills it with its zero value, and return hands it back.

Rules

  • Edit zero.go. Each function has a // TODO comment and a return line you will change.
  • In each function, declare one variable with var and its type, with no value. Then return that variable.
  • ZeroInt returns an int, ZeroFloat a float64, ZeroBool a bool, and ZeroString a string.
  • Do not type the answers yourself. Writing return 0 or var n int = 0 skips the point of the exercise, and the tests will tell you.
  • Do not change the func lines. Do not add imports. You will not need any.

What you'll practice

Declaring variables with var and no value, and seeing that Go fills them with zero values: 0, 0 for floats, false, and the empty string.

Run it

gonomicon fetch week-1/08-02-zero-values-by-default
cd 08-02-zero-values-by-default
gonomicon test
gonomicon submit

Hints

  1. You do not put anything in these boxes. Write var and a label and the type, and stop there.
  2. For the number box: var n int. Then the next line is return n. The other three boxes follow the same pattern with their own types.
  3. ZeroInt: var n int then return n. ZeroFloat: var f float64 then return f. ZeroBool: var b bool then return b. ZeroString: var s string then return s.

Something wrong with this lesson? Report a problem