Go if Statement 🎯

beginner
13 min

Go if Statement 🎯

Welcome to the exciting world of Go (Golang)! In this lesson, we'll delve into the if statement - a fundamental building block in any programming language. By the end of this tutorial, you'll have a solid understanding of how to use if statements effectively in your Go projects.

Let's start by understanding what an if statement is and why we use it. 📝

What is an if Statement?

An if statement in Go is a control structure that allows you to execute a block of code only when a certain condition is met. It helps you make decisions and control the flow of your program based on the given conditions.

Here's the basic structure of an if statement in Go:

go
if condition { // Code to be executed if condition is true }

Now, let's see an example of an if statement in action:

go
package main import "fmt" func main() { age := 20 if age >= 18 { fmt.Println("You are eligible to vote.") } }

In the above example, we're checking if the age is greater than or equal to 18. If the condition is true, we print "You are eligible to vote." ✅

if-else Statement

To make our programs more versatile, we can add an else statement to the if statement. The else block will be executed when the condition in the if statement is false. Here's the basic structure of an if-else statement:

go
if condition { // Code to be executed if condition is true } else { // Code to be executed if condition is false }

Let's modify the previous example to include an else statement:

go
package main import "fmt" func main() { age := 16 if age >= 18 { fmt.Println("You are eligible to vote.") } else { fmt.Println("You are not eligible to vote.") } }

In this example, we're checking if the age is greater than or equal to 18. If the condition is true, we print "You are eligible to vote." If the condition is false, we print "You are not eligible to vote." ✅

Nested if Statements

You can also nest if statements to create more complex conditions. Here's an example:

go
package main import "fmt" func main() { age := 20 if age >= 18 { fmt.Println("You are eligible to vote.") if age >= 21 { fmt.Println("You can also buy alcohol.") } } else { fmt.Println("You are not eligible to vote.") } }

In this example, we're first checking if the age is greater than or equal to 18. If the condition is true, we print "You are eligible to vote." Then, we check if the age is greater than or equal to 21. If the condition is true, we print "You can also buy alcohol." If the initial condition is false, we print "You are not eligible to vote." ✅

Multiple Conditions with if and elif

In Go, you can also use the elif (short for else if) keyword to check multiple conditions. Here's an example:

go
package main import "fmt" func main() { grade := 'C' if grade == 'A' { fmt.Println("Excellent! You got an A.") } else if grade == 'B' { fmt.Println("Great! You got a B.") } else if grade == 'C' { fmt.Println("Good effort! You got a C.") } else { fmt.Println("You need to improve.") } }

In this example, we're checking the grade. If the grade is 'A', we print "Excellent! You got an A." If the grade is 'B', we print "Great! You got a B." If the grade is 'C', we print "Good effort! You got a C." If none of the conditions are met, we print "You need to improve." ✅

Quick Quiz
Question 1 of 1

What does the `if` statement in Go do?

Quick Quiz
Question 1 of 1

How do you write an `if-else` statement in Go?

Quick Quiz
Question 1 of 1

What is the purpose of the `elif` keyword in Go?

Quick Quiz
Question 1 of 1

What happens when the conditions in multiple `elif` statements are true in Go?