Course / The terminal and the language
What a box holds before you fill it
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-02-zero-values-by-default
cd 08-02-zero-values-by-default
gonomicon submitGoal
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// TODOcomment and areturnline you will change. - In each function, declare one variable with
varand its type, with no value. Then return that variable. ZeroIntreturns anint,ZeroFloatafloat64,ZeroBoolabool, andZeroStringastring.- Do not type the answers yourself. Writing
return 0orvar n int = 0skips the point of the exercise, and the tests will tell you. - Do not change the
funclines. 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
- You do not put anything in these boxes. Write
varand a label and the type, and stop there. - For the number box:
var n int. Then the next line isreturn n. The other three boxes follow the same pattern with their own types. - ZeroInt:
var n intthenreturn n. ZeroFloat:var f float64thenreturn f. ZeroBool:var b boolthenreturn b. ZeroString:var s stringthenreturn s.