Conditionals and Loops

Go supports standard control flow mechanisms like if, for, and switch.

If-Else:

x := 10
if x > 5 {
    fmt.Println("x is greater than 5")
} else {
    fmt.Println("x is 5 or less")
}

For loop:

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

Switch:

switch day := "Monday";
{
case "Monday":
    fmt.Println("Start of the week")
case "Friday":
    fmt.Println("Weekend is near")
default:
    fmt.Println("Midweek")
}
← PrevNext →