Menu
Coddy logo textTech

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: false
challenge icon

Challenge

Beginner

In 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:

  1. Create a new boolean variable called isLearning and set it to true
  2. Create another boolean variable called isExpert and set it to false

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 := false

Print 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)
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals