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()
myday := today.Day()
fmt.Println(myday)
fmt.Printf("%T\n", myday)
switch today.Day() {
case 5:
fmt.Println("Buy some wine.5")
case 18:
fmt.Println("Visit a doctor.18")
case 25:
fmt.Println("Buy some food.25")
case 31:
fmt.Println("Party tonight.31")
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"
)
//حالت استفاده چند شرط در یک خط
//نکته عدد 18 در فرمت زیر به دلیل تکراری بودن مشکل دارد
func main() {
today := time.Now()
myday := today.Day()
fmt.Println(myday)
fmt.Printf("%T\n", myday)
switch today.Day() {
case 5,18:
fmt.Println("Buy some wine.5")
case 18,21:
fmt.Println("Visit a doctor.18")
case 25:
fmt.Println("Buy some food.25")
case 31:
fmt.Println("Party tonight.31")
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"
)
//fallthrough
//امروز که برنامه تست می شود روز 18 ام ماه است
//اگر از این گزینه استفاده شود علاوه بر کیس صحیح تازمانی که کیس های بعدی گزینه فال تروو را داشته باشند بدون توجه به صحیح
//بودن یا نبودن کیس درون آنها اجرا می شود
//خروجی اجرا به شکل زیر می باشد
/*
$ go run main.go
18
int
Visit a doctor.18
Buy some food.25
Party tonight.31
*/
func main() {
today := time.Now()
myday := today.Day()
fmt.Println(myday)
fmt.Printf("%T\n", myday)
switch today.Day() {
case 5:
fmt.Println("Buy some wine.5")
fallthrough
case 18, 21:
fmt.Println("Visit a doctor.18")
fallthrough
case 25:
fmt.Println("Buy some food.25")
fallthrough
case 31:
fmt.Println("Party tonight.31")
default:
fmt.Println("No information available for that day.")
}
}
Output
Below would be the output on 18th day of month.
Visit a doctor.18
Buy some food.25
Party tonight.31
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.")
}
}