C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
. A value is 10. In this case, a special value must be returned. A switch handles this.
Expressions and lists. In Go we have versatile switch statements. We can match a variable to a constant value (even in a list). Or we can match based on expressions and logic.
An example. This is a simple switch example. In the switch, we find cases—and these match expressions. So if height is less than or equal to 4, the first case is reached.
Fall-through: Cases have no fall-through. So only one case is reached, even if two are matched by the variable.
Based on: Golang 1.4 Golang program that uses switch package main import "fmt" func main() { height := 5 // Use switch on the height variable. switch { case height <= 4: fmt.Println("Short") case height <= 5: fmt.Println("Normal") case height > 5: fmt.Println("Tall") } } Output Normal
Case lists. This is another form of the switch construct. We specify a variable after the switch keyword. Then we use constants or lists of constants to match the variable.
Here: The id is equal to 10. The switch case 10 is matched and the string "Even" is printed with fmt.
Golang program that uses switch, multiple value cases package main import "fmt" func main() { id := 10 // Use switch with multiple values in each case. switch id { case 10, 12, 14: fmt.Println("Even") case 11, 13, 15: fmt.Println("Odd") } } Output Even
Return value. This is a common code pattern: we use a switch to return a value in a func. We use return statements within the cases.
Default: The default return here is specified after the switch. If nothing matches, the final return is reached.
Golang program that returns value based on switch package main import "fmt" func result(v int) int { // Return a value based on a switch. switch v { case 10, 20, 30: return v + 5 case 15, 25, 35: return v - 5 } return v } func main() { // Call the method that uses a switch. number := result(10) fmt.Println(number) number = result(25) fmt.Println(number) } Output 15 20
Fallthrough. This keyword can be used in a case in a switch statement. When fallthrough is encountered, the next case is entered (even if the expression does not match).
Golang program that uses fallthrough package main import "fmt" func test(value int) { switch(value) { case 1: // For 1, handle as 1 and fall-through to 0. fmt.Println("One") fallthrough case 0: // For 0, just print zero. fmt.Println("Zero") break } } func main() { fmt.Println(0) test(0) fmt.Println(1) test(1) } Output 0 Zero 1 One Zero
Duplicate case error. A switch's cases must be unique. Every constant, even in lists, is checked against the entire switch. A "duplicate case" error may be reported.
Golang program that causes duplicate case error package main import "fmt" func main() { value := 10 // Duplicate cases are not allowed. switch value { case 10: fmt.Println(true) case 10: fmt.Println(true) } } Output C:\programs\file.go:14: duplicate case 10 in switch previous case at C:\programs\file.go:12
A brief summary. Switches are emphasized in Go. We use them preferentially over if-statements. And most ifs can be rewritten elegantly with switch.