Golang Basics - Control Flow - If, For, and Switch

Control flow in Go is similar to other C based programming languages. Let's start with the ifstatement:

If statements

The if statement contains a condition, which should resolve to a boolean true or false, and a block of code that has to be executed if the condition is true:

if 6 < 7 {
        // This statement will print because 6 < 7 is true
        fmt.Println("why was 6 afraid of 7")
}

if 7 > 8 {
        // This statement will not print
        fmt.Println("because 7 8 9")
}

In Go, you do not need to put brackets in front of the if statement.

If-else

You can use the else keyword to execute an alternate block of code, in case the condition is false

model := "Lexus"

if model == "Toyota" {
        // this will not execute
} else {
        // this will execute
}

We can also use else if to combine multiple conditional statements

model := "Lexus"

if model == "Toyota" {
        // this will not execute
} else if model == "Lexus" {
        // this will execute
} else {
        // This block will execute if none of the above conditions are true
}

Switch statements

Instead of using the else-if ladder for control flow, like the previous example, we can use the switch statement in Go:

model := "Lexus"

switch model {
        case "Toyota":
        // this will not execute
        case "Lexus":
        // this will execute
        default:
        // this will not execute
}

You might have noticed that we did not use any break statements, as we normally do for other languages like C, Java, or Javascript. In Go, the code breaks after a case by default. If you do not want to break, you need to add a fallthrough statement:

model := "Lexus"

switch model {
        case "Toyota":
        // this will not execute
        case "Lexus":
        // this will execute
        fallthrough
        default:
        // this will execute
}

The fallthrough statement tells the code to move on to the next case if the current case is satisfied.

If we need to perform an action for multiple cases, we can combine the case blocks:

model := "Lexus"

switch model {
        case "Toyota", "Ducati":
        // this will not execute
        case "Lexus", "Ford":
        // this will execute
        default:
        // this will not execute
}

For statements

The for statement in Go is similar to other C-based languages: It consists of an initialization, a condition, and a statement that runs after each loop:

for i:=0; i< 6; i++ {
        fmt.Println(i)
}
// Output:
// 0
// 1
// 2
// 3
// 4
// 5
  • The i:0 statement initialized a new variable i to a value of 0. Here, the i variable is scoped to the for statement: that means it cannot be used outside of the for block
  • The second statement i< 6 is a condition that gets checked before the start of every loop
  • The third statement i++ executes after each loop completes

For range statement

Go has a special range keyword, that's very useful when we want to loop over data structures like maps or slices.

For slices, we can get the index of the value in the slice, and the value itself:

models := []string{"Tesla", "Ford", "BMW"}

// Here, range model returns the index, and the value for each loop iteration
for i, model := range models {
        fmt.Println(i, model)
}

// Output:
// 0 Tesla
// 1 Ford
// 2 BMW

For maps, we get the key and value in each iteration:

mileage := map[string]int{
        "BMW": 5,
        "Ford": 4,
}

for key, val := range mileage {
        fmt.Println(key, val)
}

// Output:
// BMW 5
// Ford 4

📖 Read next: Functions


Liked this article? Share it on: