UP | HOME

Go: Control Flow

Table of Contents

1 If statement

Like for, the if statement can start with a short statement to execute before the condition. Variables declared by the statement are only in scope until the end of the if. Variables declared inside an if short statement are also available inside any of the else blocks.

if answer != 42 {
        return "Wrong answer"
}
// IF with a short statement
if err := foo(); err != nil {
        panic(err)
}
// IF and ELSE statement
if v := math.Pow(2); v < 3 {
        return false
} else {
        return true
}

2 For loop

sum := 0
for i := 0; i < 10; i++ {
        sum += i
}

For loop without pre/post statement

sum := 1
for ; sum < 1000; {
        sum += sum
}

For loop as while loop

sum := 1
for sum < 1000 {
        sum += sum
}

Infinite loop

for {
        // do something in a loop forever
}

3 Switch case statement

  • You can only compare values of the same type.
  • You can set an optional default statement to be executed if all the others fail.
  • You can use an expression in the case statement, for instance you can calculate a value to use in the case:

    package main
    
    import "fmt"
    
    func main() {
            num := 3
            v := num % 2
            switch v {
            case 0:
                    fmt.Println("even")
            case 3 - 2:
                    fmt.Println("odd")
            }
    }
    
    odd
    
    
  • You can have multiple values in a case statement:

    package main
    
    import "fmt"
    
    func main() {
            score := 7
            switch score {
            case 0, 1, 3:
                    fmt.Println("Terrible")
            case 4, 5:
                    fmt.Println("Mediocre")
            case 6, 7:
                    fmt.Println("Not bad")
            case 8, 9:
                    fmt.Println("Almost perfect")
            case 10:
                    fmt.Println("hmm did you cheat?")
            default:
                    fmt.Println(score, " off the chart")
            }
    }
    
    Not bad
    
    
  • You can execute all the following statements after a match using the fallthrough statement:

    package main
    
    import "fmt"
    
    func main() {
            n := 4
            switch n {
            case 0:
                    fmt.Println("is zero")
                    fallthrough
            case 1:
                    fmt.Println("is <= 1")
                    fallthrough
            case 2:
                    fmt.Println("is <= 2")
                    fallthrough
            case 3:
                    fmt.Println("is <= 3")
                    fallthrough
            case 4:
                    fmt.Println("is <= 4")
                    fallthrough
            case 5:
                    fmt.Println("is <= 5")
                    fallthrough
            case 6:
                    fmt.Println("is <= 6")
                    fallthrough
            case 7:
                    fmt.Println("is <= 7")
                    fallthrough
            case 8:
                    fmt.Println("is <= 8")
                    fallthrough
            default:
                    fmt.Println("Try again!")
            }
    }
    
    is <= 4
    is <= 5
    is <= 6
    is <= 7
    is <= 8
    Try again!
    
    
  • You can use a break statement inside your matched statement to exit the switch processing:

    package main
    
    import (
            "fmt"
            "time"
    )
    
    func main() {
            n := 1
            switch n {
            case 0:
                    fmt.Println("is zero")
                    fallthrough
            case 1:
                    fmt.Println("<= 1")
                    fallthrough
            case 2:
                    fmt.Println("<= 2")
                    fallthrough
            case 3:
                    fmt.Println("<= 3")
                    if time.Now().Unix()%2 == 0 {
                            fmt.Println("un pasito pa lante maria")
                            break
                    }
                    fallthrough
            case 4:
                    fmt.Println("<= 4")
                    fallthrough
            case 5:
                    fmt.Println("<= 5")
            }
    }
    
    <= 1
    <= 2
    <= 3
    <= 4
    <= 5
    
    

Author: Pavel Vavilin

Created: 2017-10-26 Thu 21:21

Validate