Learn Go: Mastering Loops, Conditionals, and Switch Statements

ยท

4 min read

Cover Image for Learn Go: Mastering Loops, Conditionals, and Switch Statements

As a beginner in Go programming, understanding control structures is crucial for writing efficient and readable code. In this blog post, we'll dive deep into Go's loops, conditional statements, and switch statements. These fundamental concepts will help you control the flow of your programs and make them more dynamic.

Loops in Go: The Power of 'for'

Go simplifies looping by using a single keyword: for. This versatile construct can be used to create various types of loops.

While-style Loop

In Go, we can create a while-style loop using the for keyword:

i := 1
for i <= 5 {
    fmt.Println(i)
    i = i + 1
}

This loop continues as long as the condition i <= 5 is true.

Classic For Loop

The traditional C-style for loop is also available in Go:

for i := 20; i < 28; i++ {
    if i == 25 {
        continue
    }
    fmt.Println(i + 5)
}

Here, we initialize i to 20, continue while i < 28, and increment i after each iteration.

Range-based Loop

Go provides a convenient way to iterate over slices, arrays, maps, or channels using the range keyword:

for i := range 5 {
    fmt.Println(i)
}

This loop iterates 5 times, with i taking values from 0 to 4.

Infinite Loop

While not recommended in most cases, you can create an infinite loop in Go:

for {
    fmt.Println("This will run forever!")
}

Be cautious when using infinite loops and ensure you have a way to break out of them.

Conditional Statements: Making Decisions

Conditional statements allow your program to make decisions based on certain conditions.

If-Else Statements

Go's if-else statements are straightforward:

age := 24

if age >= 18 {
    fmt.Println("Person is an adult!")
} else {
    fmt.Println("Kid")
}

You can also use else-if for multiple conditions:

barAge := 18
personAge := 18

if personAge > barAge {
    fmt.Println("Welcome to bar")
} else if personAge == barAge {
    fmt.Println("You are 18, welcome to bar")
} else {
    fmt.Println("Not allowed to bar")
}

Logical Operators in Conditionals

Go supports logical operators like && (AND) and || (OR) in conditional statements:

role := "admin"
hasPermissions := false

if role == "admin" || hasPermissions {
    fmt.Println("allowed")
} else {
    fmt.Println("Not allowed")
}

Declaring Variables in If Statements

Go allows you to declare variables within the if statement itself:

if age := 14; age >= 18 {
    fmt.Println("Person is adult", age)
} else if age >= 12 {
    fmt.Println("Person is teenager", age)
}

This is useful for limiting the scope of variables used only in the conditional block.

Switch Statements: Simplifying Multiple Conditions

Switch statements in Go provide a clean way to handle multiple conditions.

Basic Switch

Here's a simple switch statement to determine the day of the week:

i := 4

switch i {
case 1:
    fmt.Println("Sunday")
case 2:
    fmt.Println("Monday")
case 3:
    fmt.Println("Tuesday")
case 4:
    fmt.Println("Wednesday")
case 5:
    fmt.Println("Thursday")
case 6:
    fmt.Println("Friday")
case 7:
    fmt.Println("Saturday")
default:
    fmt.Println("Not a weekday")
}

Multiple Conditions in a Case

Go allows multiple values in a single case:

switch time.Now().Weekday() {
case time.Saturday, time.Sunday:
    fmt.Println("Weekend")
default:
    fmt.Println("Work day")
}

Type Switch

Go's type switch is a powerful feature for handling different types:

whoAmI := func(i interface{}) {
    switch t := i.(type) {
    case int:
        fmt.Println("Integer")
    case string:
        fmt.Println("String")
    case bool:
        fmt.Println("Boolean")
    default:
        fmt.Printf("Other type: %T\n", t)
    }
}

whoAmI("S Soumyakanta")
whoAmI(3.3)
whoAmI(true)

This type switch determines the type of the argument passed to the whoAmI function.

Conclusion

Mastering Go's control structures is essential for writing efficient and readable code. The versatility of the for loop, the clarity of if-else statements, and the power of switch statements make Go a joy to work with. As you continue your journey with Go, remember that practice is key. Experiment with these structures in your projects to gain a deeper understanding of how they work and when to use each one.

By leveraging these control structures effectively, you'll be well on your way to becoming a proficient Go programmer. Happy coding!