Skip to main content

Command Palette

Search for a command to run...

Conditional - If, Else if, Else

Updated
1 min readView as Markdown
Conditional - If, Else if, Else
S
DevRel at StackGen | Formerly at Deepfence ,Tenable , Accurics | AWS Community Builder also Docker Community Award Winner at Dockercon2020 | CyberSecurity Innovator of Year 2023 award by Bsides Bangalore | Docker/HashiCorp Meetup Organiser Bangalore & Co-Author of Learn Lightweight Kubernetes with k3s (2019) , Packt Publication & also run Non Profit CloudNativeFolks / CloudSecCorner Community To Empower Free Education reach out me twitterhttps://twitter.com/sangamtwts or just follow on GitHub -> https://github.com/sangam14 for Valuable Resources

Conditional - If, Else if, Else

Here's an example of using if and else

package main

import (
    "fmt"
)

func main() {
    x := 42

    if x == 40 {
        fmt.Println("Our value was 40")
    } else {
        fmt.Println("Our value was not 40")
    }
}

playground

We can also use else if as many times as we want within the if statement.

package main

import (
    "fmt"
)

func main() {
    x := 42
    if x == 40 {
        fmt.Println("Our value was 40")
    } else if x == 41 {
        fmt.Println("Our value was 41")
    } else if x == 42 {
        fmt.Println("Our value was 42")
    } else if x == 43 {
        fmt.Println("Our value was 43")
    } else {
        fmt.Println("Our value was not 40")
    }
}

playground

43 views

More from this blog

C

CloudNativeFolks Community

140 posts

CloudNativeFolks is non profit community that empower and educate about cloud native technology !