DEV Community

Sukma Rizki
Sukma Rizki

Posted on

Interesting Control Flow in the circle

in Go (Golang), control flow is managed using several fundamental constructs, including conditionals (if, else), loops (for), and switch statements. Here's an overview of how these constructs work in Go:

  1. Conditionals: if, else, else if In Go, if statements are used to execute code based on a condition. Unlike some other languages, Go doesn't require parentheses around the condition. However, the curly braces {} are mandatory.

Basic Statement

package main

import "fmt"

func main() {
    age := 20

    if age >= 18 {
        fmt.Println("You are an adult.")
    }
}

Enter fullscreen mode Exit fullscreen mode

'if-else statement' Example
`package main

import "fmt"

func main() {
age := 16

if age >= 18 {
    fmt.Println("You are an adult.")
} else {
    fmt.Println("You are not an adult.")
}
Enter fullscreen mode Exit fullscreen mode

}
`
'if-else if-else' Statement:

package main

import "fmt"

func main() {
    age := 20

    if age >= 21 {
        fmt.Println("You can drink alcohol.")
    } else if age >= 18 {
        fmt.Println("You are an adult, but cannot drink alcohol.")
    } else {
        fmt.Println("You are not an adult.")
    }
}

Enter fullscreen mode Exit fullscreen mode

2.Loops: for
Go uses the 'for' loop for all looping needs; it does not have a 'while' or loop
basic 'for' loop:

package main

import "fmt"

func main() {
    for i := 0; i < 5; i++ {
        fmt.Println(i)
    }
}

Enter fullscreen mode Exit fullscreen mode

'for' as a 'while' loop:

package main

import "fmt"

func main() {
    i := 0
    for i < 5 {
        fmt.Println(i)
        i++
    }
}

Enter fullscreen mode Exit fullscreen mode

Infinite Loop:

package main

func main() {
    for {
        // This loop will run forever
    }
}

Enter fullscreen mode Exit fullscreen mode

'for' loop with 'range':
This is often used to iterate over slices,arrays,maps or strings.

package main

import "fmt"

func main() {
    numbers := []int{1, 2, 3, 4, 5}

    for index, value := range numbers {
        fmt.Println("Index:", index, "Value:", value)
    }
}

Enter fullscreen mode Exit fullscreen mode
  1. Switch Statement Go The 'Switch' statement in Go is used to select one of many code blocks to be executed.Go 'switch'is more powerful than in some other languages and can be used with any type of value, not just integers.

Basic 'switch'

package main

import "fmt"

func main() {
    day := "Monday"

    switch day {
    case "Monday":
        fmt.Println("Start of the work week.")
    case "Friday":
        fmt.Println("End of the work week.")
    default:
        fmt.Println("Midweek.")
    }
}

Enter fullscreen mode Exit fullscreen mode

Switch with multiple expressions in a case:

package main

import "fmt"

func main() {
    day := "Saturday"

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

Enter fullscreen mode Exit fullscreen mode

A switch with no expression acts like a chain of if-else statements.

package main

import "fmt"

func main() {
    age := 18

    switch {
    case age < 18:
        fmt.Println("Underage")
    case age >= 18 && age < 21:
        fmt.Println("Young adult")
    default:
        fmt.Println("Adult")
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. Defer, panic and recover
package main

import "fmt"

func main() {
    defer fmt.Println("This is deferred and will run at the end.")
    fmt.Println("This will run first.")
}

Enter fullscreen mode Exit fullscreen mode

Panic And Recover

package main

import "fmt"

func main() {
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Recovered from panic:", r)
        }
    }()

    fmt.Println("About to panic!")
    panic("Something went wrong.")
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)