Golang Switch Examples: case, break and fallthrough
Use switch, with expressions and case lists, to test variables and return values.Switch. Often a selection must be made based on the value of a variable. A value is 10. In this case, a special value must be returned. A switch handles this.
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.
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.
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).
Test 0: Test() is called with the value of 0. The case 0 is reached in test(), and the func is done.
Test 1: The test() method is called next with a value of 1. Case 1 is reached, and then case 0 is entered because control "falls through."
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
Floating-point. In some languages a float cannot be used in a switch. But in Go we can switch on floating-point numbers. This program shows a switch on a variable with value 2.5.
Golang program that switches on floating-point number
package main
import "fmt"
func main() {
value := 2.5
// Switch on a floating-point value.
switch value {
case 1.5:
fmt.Println("One point five")
case 2.5:
fmt.Println("Two point five")
}
}
Output
Two point five
Default. A switch can have a default case. This is reached when no other case matches. A switch can have 0 or 1 default cases, and the position in the switch statement is not important.
Here: The value is equal to 5, so the case 4 does not match. The default case is instead reached.
Golang program that uses switch, default
package main
import "fmt"
func main() {
value := 5
// Switch on the value.
switch value {
case 4:
fmt.Println("FOUR")
default:
// The default case is reached.
fmt.Println("UNKNOWN")
}
}
Output
UNKNOWN
String switch. The Go language supports switch on strings. We can specify string literals in the case statements. The strings must exactly match.
Golang program that uses string switch
package main
import "fmt"
func main() {
animal := "cat"
// Switch on string.
switch animal {
case "bird":
fmt.Println("MATCHED BIRD")
case "cat":
fmt.Println("MATCHED CAT")
}
}
Output
MATCHED CAT
Performance, if versus switch. Does the switch statement provide a huge performance boost over if? I tested a simple integer switch against an equivalent if-else chain.
Result: I ran the programs separately to try to eliminate interference. And the results were close.
Analysis: Switch and if are about the same speed on small integer groups. Use the version that is clearest and easiest to read.
Golang program that benchmarks switch
package main
import (
"fmt"
"time"
)
func main() {
result := 0
t0 := time.Now()
// Test switch statement.
for i := 0; i < 10000000; i++ {
for v := 0; v < 5; v++ {
switch v {
case 0:
result += 1
case 1, 2, 3:
result += 2
case 4:
result += 3
}
}
}
t1 := time.Now()
// Result values.
fmt.Println(result)
fmt.Println(t1.Sub(t0))
}
Golang program that benchmarks if, else
package main
import (
"fmt"
"time"
)
func main() {
result := 0
t0 := time.Now()
// Test if-statement.
for i := 0; i < 10000000; i++ {
for v := 0; v < 5; v++ {
if v == 0 {
result += 1
} else if v == 1 || v == 2 || v == 3 {
result += 2
} else if v == 4 {
result += 3
}
}
}
t1 := time.Now()
// Result values.
fmt.Println(result)
fmt.Println(t1.Sub(t0))
}
Output
71.0622ms Switch
70.0615ms
71.0636ms
70.062 ms If
70.0487ms
70.05 ms
Type switch. With a type switch we can detect what interfaces a variable implements. We use the (type) syntax to get the type and pass it to the switch. Then we have interface cases.
Tip: We use the switch on an interface{} variable in the test() method. We then use (type).
InterfaceTip 2: The cases must have interfaces that are implemented on the type. The inner statements of the case are then executed.
Quote: [Cases] match actual types T against the dynamic type of the expression x. As with type assertions, x must be of interface type, and each non-interface type T listed in a case must implement the type of x.
Go Language Specification: golang.org Golang program that uses type switch
package main
import "fmt"
type Page interface {
PrintPage()
}
type HtmlPage struct {
// Implement Page interface.
Page
}
type Image interface {
PrintImage()
}
type ImagePage struct {
// Implement Image interface.
Image
}
func test(value interface{}) {
// Use type switch to test interface type.
// ... The argument is an interface.
switch value.(type) {
case nil:
fmt.Println("Is nil interface")
case Page:
fmt.Println("Is page interface");
case Image:
fmt.Println("Is image interface");
}
}
func main() {
// Create class that implements interface and pass to test func.
item1 := new(HtmlPage)
test(item1)
item2 := new(ImagePage)
test(item2)
}
Output
Is page interface
Is image interface
A brief summary. Switches are emphasized in Go. We use them preferentially over if-statements. And most ifs can be rewritten elegantly with switch.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.