C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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
Here: The id is equal to 10. The switch case 10 is matched and the string "Even" is printed with fmt.
fmtGolang 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
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
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
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
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
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
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
Version 1: This version of the code uses the switch statement. The cases in the switch are all encountered.
Version 2: Here we use the if-statement instead of switch. The if-statements do the same thing as the switch.
Result: In newer versions of Go, we find a significant benefit to the switch statement. On numbers, prefer switch when possible.
Golang program that benchmarks switch
package main
import (
"fmt"
"time"
)
func main() {
Version1()
Version2()
}
func Version1() {
result := 0
t0 := time.Now()
// Version 1: 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()
// Results.
fmt.Println(result)
fmt.Println(t1.Sub(t0))
}
func Version2() {
result := 0
t0 := time.Now()
// Version 2: 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()
// Results.
fmt.Println(result)
fmt.Println(t1.Sub(t0))
}
Output
100000000
53.8862ms Switch
100000000
82.807ms If
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.orgGolang 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