If/Else in Go

Go: If/Else

If/Else in Go is very straightforward and similar to other languages like javascript etc.

Here is a basic example of if/else

    package main
    import "fmt"
    func main() {
        
        //Here’s a basic example.
        if 7%2 == 0 {
            fmt.Println("7 is even")
        } else {
            fmt.Println("7 is odd")
        }

        //if statement without an else.

        if 8%4 == 0 {
            fmt.Println("8 is divisible by 4")
        }
        
        // if statement with declaration of variable
        // variable will be persis in the current and all subsequent branches.
        if num := 9; num < 0 {
            fmt.Println(num, "is negative")
        } else if num < 10 {
            fmt.Println(num, "has 1 digit")
        } else {
            fmt.Println(num, "has multiple digits")
        }
    }

Previous Article

Next Article