Go Conditionals

if / else

Go uses C-style conditionals, but parentheses are not required around conditions.

Loading...
Output:

if with short statement

You can include a short variable declaration before the condition:

Loading...
Output:

This is useful for temporary scoped values.

else if

temp := 30
if temp < 0 {
    fmt.Println("Freezing")
} else if temp < 25 {
    fmt.Println("Cool")
} else {
    fmt.Println("Hot")
}

switch Statement

Switch is cleaner than multiple if-else. It doesn't fall through by default.

Loading...
Output:

switch with condition

You can also switch on conditions directly (like if-else chaining):

Loading...
Output:

fallthrough keyword

By default, Go does not fall through to the next case. If needed, use fallthrough:

Loading...
Output:

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic