Executes a block of code when a condition is true.
if condition { // code runs if condition is true} else if anotherCondition { // code runs if it gets here and anotherCondition is true} else { // code runs if none of the above are true}
Go lets you declare a variable that only exists in the if and else blocks:
s := "Hello"if n := len(s); n == 1 { fmt.Println("Length is", n)} else if n >= 2 { fmt.Println("Length is", n)} else { fmt.Println("Empty string")}// Length is 5fmt.Println("Length is", n) // undefined: n (compiler)
switch expression {case value1: // code runs if expression == value1case value2: // code runs if expression == value2case value3, value4: // code runs if expression == value3 or value4default: // code runs if expression does not match any case}
Go auto break after each case implicitly, but the keyword can still be used.
Since Go breaks after each case by default, the fallthrough keyword allows you to explicitly override this behavior when sequential processing is needed.
a := 2switch a {case 1: fmt.Println("One")case 2: fmt.Println("Two") fallthroughcase 3: fmt.Println("Three")case 4: fmt.Println("Four")default: fmt.Println("None of above")}// Two// Three
Omitting the expression makes the switch statement act like an if-else statement:
a := 3switch {case a == 1: fmt.Println("One")case a == 2: fmt.Println("Two")case a == 3 || a == 4: fmt.Println("Three or Four")default: fmt.Println("None of above")}// Three or Four