The switch
statement in Go is used for decision-making, similar to if-else
, but it is more readable when multiple conditions need to be checked. It allows a variable to be tested against multiple values, executing the corresponding case when a match is found.
switch expression {
case value1:
// Code to execute if expression == value1
case value2:
// Code to execute if expression == value2
default:
// Code to execute if no cases match
}
đš Key Points:
â
No break
statement is needed (unlike C, C++). Once a matching case executes, it automatically exits the switch block.
â
default
case is optional, but it is used when no other case matches.
package main
import "fmt"
func main() {
day := "Monday"
switch day {
case "Monday":
fmt.Println("Start of the work week!")
case "Friday":
fmt.Println("Weekend is near!")
default:
fmt.Println("It's a regular day.")
}
}
đš Output:
Start of the work week!
đ Explanation:
day == "Monday"
, so the first case
executes.switch
after that.Go allows multiple cases to share the same block of code.
package main
import "fmt"
func main() {
day := "Saturday"
switch day {
case "Saturday", "Sunday":
fmt.Println("It's the weekend!")
default:
fmt.Println("It's a weekday.")
}
}
đš Output:
It's the weekend!
đ Explanation:
day == "Saturday"
, which matches case "Saturday", "Sunday"
, so the message is printed.In Go, a switch
can work without an expression, making it similar to an if-else
ladder.
package main
import "fmt"
func main() {
num := -5
switch {
case num > 0:
fmt.Println("Positive number")
case num < 0:
fmt.Println("Negative number")
default:
fmt.Println("Zero")
}
}
đš Output:
Negative number
đ Explanation:
num < 0
, the second case executes.By default, Go does not fall through cases like C/C++. However, you can force execution of the next case using the fallthrough
keyword.
package main
import "fmt"
func main() {
num := 1
switch num {
case 1:
fmt.Println("Case 1 executed")
fallthrough
case 2:
fmt.Println("Case 2 executed")
default:
fmt.Println("Default case")
}
}
đš Output:
Case 1 executed
Case 2 executed
đ Explanation:
num == 1
, so case 1
executes.fallthrough
forces case 2
to execute as well, even though num
is not 2
.A type switch is used when you want to check the type of a variable stored in an interface{}
.
package main
import "fmt"
func typeCheck(i interface{}) {
switch v := i.(type) {
case int:
fmt.Println("It's an integer:", v)
case string:
fmt.Println("It's a string:", v)
default:
fmt.Println("Unknown type")
}
}
func main() {
typeCheck(10)
typeCheck("GoLang")
typeCheck(3.14)
}
đš Output:
It's an integer: 10
It's a string: GoLang
Unknown type
đ Explanation:
i
and executes the corresponding case.int
and string
are matched, but 3.14
(float) falls into default
.Feature | Switch | If-Else |
---|---|---|
Readability | Better for multiple conditions | Becomes lengthy with many conditions |
Performance | Faster (compiles to jump table) | Can be slower for many conditions |
Type Checking | Can be used for type switches | Requires explicit type checks |
Fallthrough | Disabled by default | Not applicable |
â
switch
is more readable than multiple if-else
statements.
â
No break
is needed (it exits automatically).
â
You can have multiple values in one case.
â
Type switches allow checking variable types.
â
fallthrough
forces execution of the next case.
đ Use switch
for cleaner and faster conditional checks!
switch
āϏā§āĻā§āĻāĻŽā§āύā§āĻ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰ⧠āĻāĻāĻāĻŋ āĻā§āϰāĻŋāϝāĻŧā§āĻŦāϞ āĻŦāĻŋāĻāĻŋāύā§āύ āĻŽāĻžāύā§āϰ āϏāĻžāĻĨā§ āϤā§āϞāύāĻž āĻāϰ⧠āύāĻŋāϰā§āĻĻāĻŋāώā§āĻ āĻā§āĻĄ āĻŦā§āϞāĻ āĻāĻžāϞāĻžāύ⧠āϝāĻžāϝāĻŧāĨ¤ āĻāĻāĻŋ if-else
-āĻāϰ āĻā§āϝāĻŧā§ āϏāĻšāĻ āĻ āĻĻā§āϰā§āϤāϤāϰ āϏāĻŽāĻžāϧāĻžāύāĨ¤
switch expression {
case value1:
// āϝāĻĻāĻŋ expression == value1 āĻšāϝāĻŧ
case value2:
// āϝāĻĻāĻŋ expression == value2 āĻšāϝāĻŧ
default:
// āϝāĻĻāĻŋ āĻāĻŋāĻā§āĻ āύāĻž āĻŽā§āϞā§
}
â āĻāĻĻāĻžāĻšāϰāĻŖ:
package main
import "fmt"
func main() {
day := "Monday"
switch day {
case "Monday":
fmt.Println("āϏāĻĒā§āϤāĻžāĻšā§āϰ āĻļā§āϰā§!")
case "Friday":
fmt.Println("āϏāĻĒā§āϤāĻžāĻšāĻžāύā§āϤ āĻāĻžāĻāĻžāĻāĻžāĻāĻŋ!")
default:
fmt.Println("āϏāĻžāϧāĻžāϰāĻŖ āĻĻāĻŋāύāĨ¤")
}
}
đš āĻāĻāĻāĻĒā§āĻ:
āϏāĻĒā§āϤāĻžāĻšā§āϰ āĻļā§āϰā§!
case "Saturday", "Sunday":
fmt.Println("āĻāĻāĻŋ āĻā§āĻāĻŋāϰ āĻĻāĻŋāύ!")
â āĻāĻĻāĻžāĻšāϰāĻŖ:
day := "Sunday"
switch day {
case "Saturday", "Sunday":
fmt.Println("āĻāĻāĻŋ āĻā§āĻāĻŋāϰ āĻĻāĻŋāύ!")
default:
fmt.Println("āĻāĻāĻŋ āĻāϰā§āĻŽāĻĻāĻŋāĻŦāϏāĨ¤")
}
đš āĻāĻāĻāĻĒā§āĻ:
āĻāĻāĻŋ āĻā§āĻāĻŋāϰ āĻĻāĻŋāύ!
fallthrough
num := 1
switch num {
case 1:
fmt.Println("Case 1")
fallthrough
case 2:
fmt.Println("Case 2")
}
đš āĻāĻāĻāĻĒā§āĻ:
Case 1
Case 2
type switch
)func typeCheck(i interface{}) {
switch i.(type) {
case int:
fmt.Println("āĻāĻāĻāĻŋ āĻāĻāĻāĻŋ āϏāĻāĻā§āϝāĻžāĨ¤")
case string:
fmt.Println("āĻāĻāĻāĻŋ āĻāĻāĻāĻŋ āϏā§āĻā§āϰāĻŋāĻāĨ¤")
}
}
â āϏāĻāĻā§āώā§āĻĒā§:
switch
āĻ
āύā§āĻ if-else
āϏā§āĻā§āĻāĻŽā§āύā§āĻāĻā§ āϏāĻšāĻ āĻāϰā§āĨ¤fallthrough
āĻļā§āϧā§āĻŽāĻžāϤā§āϰ āĻĒāϰāĻŦāϰā§āϤ⧠case
āĻāĻžāϞāĻžāύā§āϰ āĻāύā§āϝ āĻŦā§āϝāĻŦāĻšā§āϤ āĻšāϝāĻŧāĨ¤type switch
āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰāĻž āϝāĻžāϝāĻŧāĨ¤đ Go-āϤ⧠switch
āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰā§āύ āĻāϰāĻ āĻāĻžāϞ⧠āĻĒāĻžāϰāĻĢāϰāĻŽā§āϝāĻžāύā§āϏā§āϰ āĻāύā§āϝ!