Zero Values
Part of the Fundamentals section of Coddy's GO journey — lesson 11 of 109.
In Go, zero values are default values assigned to variables when declared without an explicit initial value. Let's explore these default values for different data types.
First, let's declare variables of different types without initializing them:
var number int
var decimal float64
var isValid bool
var name string
fmt.Println(number)
fmt.Println(decimal)
fmt.Println(isValid)
fmt.Println(name)After executing this code, we'll see the zero values for each type will show on the screen:
0
0
false
Notice that int and float64 have a zero value of 0, bool has a zero value of false, and string has a zero value of an empty string (which appears as a blank line in the output).
Challenge
BeginnerIn this challenge, you'll explore Go's zero values - the default values that variables get when they're declared but not explicitly initialized.
Your task is to declare four variables (one for each basic type: int, float64, bool, and string) without initializing them, then print their zero values.
The code template already has the variable declarations set up for you. You just need to print each variable to see its zero value.
Cheat sheet
In Go, zero values are default values assigned to variables when declared without an explicit initial value:
var number int
var decimal float64
var isValid bool
var name stringZero values for different data types:
int:0float64:0bool:falsestring: empty string""
Try it yourself
package main
import "fmt"
func main() {
// These variables are declared but not initialized
// Go will automatically assign them their zero values
var number int
var decimal float64
var isTrue bool
var text string
// TODO: Print each variable to see its zero value
// Hint: Use fmt.Println() for each variable
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions