Learn
Learn Go: Variables and Types
Zero Values

Even before we assign anything to our variables they hold a value. Go’s designers attempted to make these “sensible defaults” that we can anticipate based on the variable’s types. All numeric variables have a value of 0 before assignment. String variables have a default value of "", which is also known as the empty string because it contains no characters. Boolean variables have a default value of false. For example:

var classTime uint32 var averageGrade float32 var teacherName string var isPassFail bool fmt.Println(classTime) // Prints 0 fmt.Println(averageGrade) // Prints 0 fmt.Println(teacherName) // Doesn't print anything fmt.Println(isPassFail) // Prints false

Above we declared four variables, an unsigned 32-bit int, a 32-bit floating point number, a string, and a boolean. Without assigning any of the variables to a value we print them out to see their default value. The two numbers print the same result, 0, a valid value for both types. The empty string, when printed, displays nothing. The boolean value prints false.

Instructions

1.

Create a variable emptyInt that’s of type int8.

2.

Create a variable called emptyFloat that’s of type float32.

3.

Create an empty string called emptyString.

4.

Now print out all three variables on the same line. Since they’re not all strings you’ll have to separate them by commas, like so:

fmt.Println(emptyInt, emptyFloat, emptyString)
Folder Icon

Take this course for free

Already have an account?