Golang Tutorial
Introduction Variables Constants Data Type Convert Types Operators If..Else Switch..Case For Loops Functions Variadic Functions Deferred Functions Calls Panic and Recover Arrays Slices Maps Struct Interface Goroutines Channels Concurrency Problems Logs Files and Directories Reading and Writing Files Regular Expression Find DNS records Cryptography Gotchas in Golang Import and Export Best Golang Packages Web Application Goroutines and Channels Exercises Reflection in Golang Golang for beginners Strings in Golang HTTP Client Server Examples Context PackageGolang Reference
Basic Programs Advance Programs Data Structure and Algorithms Date and Time Slice Sort, Reverse, Search Functions String Functions Methods and Objects Interface TypeBeego Framework
Beego Setup Beego Database Migration Beego GET POST Beego RoutingGolang Switch…Case Statements
Switch Case
In this tutorial you will learn how to use the switch-case statement to perform different actions based on different conditions in Golang.
Golang also supports a switch statement similar to that found in other languages such as, Php or Java. Switch statements are an alternative way to express lengthy if else comparisons into more readable code based on the state of a variable.
Golang - switch Statement
The switch statement is used to select one of many blocks of code to be executed.
Consider the following example, which display a different message for particular day.
Example
package main
import (
"fmt"
"time"
)
func main() {
today := time.Now()
switch today.Day() {
case 5:
fmt.Println("Today is 5th. Clean your house.")
case 10:
fmt.Println("Today is 10th. Buy some wine.")
case 15:
fmt.Println("Today is 15th. Visit a doctor.")
case 25:
fmt.Println("Today is 25th. Buy some food.")
case 31:
fmt.Println("Party tonight.")
default:
fmt.Println("No information available for that day.")
}
}
The default statement is used if no match is found.
Golang - switch multiple cases Statement
The switch with multiple case line statement is used to select common block of code for many similar cases.
Example
package main
import (
"fmt"
"time"
)
func main() {
today := time.Now()
var t int = today.Day()
switch t {
case 5, 10, 15:
fmt.Println("Clean your house.")
case 25, 26, 27:
fmt.Println("Buy some food.")
case 31:
fmt.Println("Party tonight.")
default:
fmt.Println("No information available for that day.")
}
}
Golang - switch fallthrough case Statement
The fallthrough keyword used to force the execution flow to fall through the successive case block.
Example
package main
import (
"fmt"
"time"
)
func main() {
today := time.Now()
switch today.Day() {
case 5:
fmt.Println("Clean your house.")
fallthrough
case 10:
fmt.Println("Buy some wine.")
fallthrough
case 15:
fmt.Println("Visit a doctor.")
fallthrough
case 25:
fmt.Println("Buy some food.")
fallthrough
case 31:
fmt.Println("Party tonight.")
default:
fmt.Println("No information available for that day.")
}
}
Output
Below would be the output on 10th day of month.
Buy some wine.
Visit a doctor.
Buy some food.
Party tonight.
Golang - swith conditional cases Statement
The case statement can also used with conditional operators.
Example
package main
import (
"fmt"
"time"
)
func main() {
today := time.Now()
switch {
case today.Day() < 5:
fmt.Println("Clean your house.")
case today.Day() <= 10:
fmt.Println("Buy some wine.")
case today.Day() > 15:
fmt.Println("Visit a doctor.")
case today.Day() == 25:
fmt.Println("Buy some food.")
default:
fmt.Println("No information available for that day.")
}
}
Golang - switch initializer Statement
The switch keyword may be immediately followed by a simple initialization statement where variables, local to the switch code block, may be declared and initialized.
Example
package main
import (
"fmt"
"time"
)
func main() {
switch today := time.Now(); {
case today.Day() < 5:
fmt.Println("Clean your house.")
case today.Day() <= 10:
fmt.Println("Buy some wine.")
case today.Day() > 15:
fmt.Println("Visit a doctor.")
case today.Day() == 25:
fmt.Println("Buy some food.")
default:
fmt.Println("No information available for that day.")
}
}