If-Else/Switch Statements in Go

If-Else/Switch Statements in Go

If Statement

if 2>1 {
    fmt.Println("2 is larger than 1")
}
// Outputs: 2 is larger than 1

If-Else Statement

a := 2
b := 3
if a>b {
    fmt.Println("a is larger than b")
} else {
    fmt.Println("b is larger than a")
}
// Outputs: b is larger than a

Else-If Statement

a := 1
b := 1
if a>b {
    fmt.Println("a is larger than b")
} else if b>a {
    fmt.Println("b is larger than a")
} else {
    fmt.Println("They are equal!")
}
// Outputs: They are equal! 

Switch Statement

a := 3
switch a {
    case 0:
        fmt.Println("a is 0")
    case 1:
        fmt.Println("a is 1")
    case 2:
        fmt.Println("a is 2")
    default:
        fmt.Println("a is none of those")
} 
// Outputs: a is none of those
Last updated on