Booleans
Part of the Fundamentals section of Coddy's GO journey — lesson 9 of 109.
Booleans in Go store true/false values using the bool data type.
Let's declare a boolean variable using the := short declaration syntax:
isActive := true
fmt.Println("Is active:", isActive)After executing this code, the value Is active: true will show on the screen. We've created a boolean variable that holds the value true.
Now, let's create another boolean variable with a false value:
isComplete := false
fmt.Println("Is complete:", isComplete)After executing this code, the value Is complete: false will show on the screen. This demonstrates that boolean variables can hold either true or false values.
Let's see what happens when we print both boolean variables together:
isActive := true
isComplete := false
fmt.Println("Is active:", isActive)
fmt.Println("Is complete:", isComplete)After executing this code, we'll see both values displayed one after another on the screen:
Is active: true
Is complete: falseChallenge
BeginnerIn this challenge, you'll work with boolean values in Go. Boolean values can only be true or false.
We've started with two boolean variables: likesGo and firstProgram. Your task is to:
- Create a new boolean variable called
isLearningand set it totrue - Create another boolean variable called
isExpertand set it tofalse
Then print all four boolean variables to see their values.
Cheat sheet
Booleans in Go store true/false values using the bool data type.
Declare boolean variables:
isActive := true
isComplete := falsePrint boolean values:
fmt.Println("Is active:", isActive)
fmt.Println("Is complete:", isComplete)Boolean variables can only hold true or false values.
Try it yourself
package main
import "fmt"
func main() {
// Two boolean variables are already defined
likesGo := true
firstProgram := false
// TODO: Create a boolean variable called 'isLearning' and set it to true
// TODO: Create a boolean variable called 'isExpert' and set it to false
// Print all the boolean variables
fmt.Println("Likes Go:", likesGo)
fmt.Println("First program:", firstProgram)
fmt.Println("Is learning:", isLearning)
fmt.Println("Is expert:", isExpert)
}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