In Go, a switch
statement is a multiway branch statement that efficiently directs execution based on the value (or type) of an expression. There are two main types of switch statements in Go:
- Expression Switch
- Type Switch
Example
package main
import "fmt"
func main() {
day := 4
switch day {
case 1:
fmt.Println("Monday")
case 2:
fmt.Println("Tuesday")
case 3:
fmt.Println("Wednesday")
case 4:
fmt.Println("Thursday")
case 5:
fmt.Println("Friday")
default:
fmt.Println("Invalid day")
}
}
Syntax
switch optstatement; optexpression {
case expression1:
// Code block
case expression2: # Expression Switch
// Code block
default:
// Code block
}
switch var := interfaceValue.(type) {
case type1:
// Code block
case type2: # Type Switch
// Code block
default:
// Code block
}
Expression Switch
The Expression Switch evaluates an expression and branches to a case based on the value of that expression. If no expression is provided, switch
defaults to true
.
Syntax
switch optstatement; optexpression {
case expression1:
// Code block
case expression2:
// Code block
default:
// Code block
}
- optstatement: Optional statement (e.g., variable declaration).
- optexpression: Optional expression (if omitted, it defaults to
true
).
Example with Optional Statement
Here, we introduce an optional statement that declares a variable day
. The switch statement then evaluates day
against various cases.
Go
package main
import "fmt"
func main() {
switch day := 4; day {
case 1:
fmt.Println("Monday")
case 2:
fmt.Println("Tuesday")
case 3:
fmt.Println("Wednesday")
case 4:
fmt.Println("Thursday")
case 5:
fmt.Println("Friday")
default:
fmt.Println("Invalid day")
}
}
Example with Optional Expression
If no expression is specified, the switch
statement assumes the expression is true
. This allows us to use boolean conditions in the case statements.
Go
package main
import "fmt"
func main() {
day := 4
switch {
case day == 1:
fmt.Println("Monday")
case day == 4:
fmt.Println("Thursday")
case day > 5:
fmt.Println("Weekend")
default:
fmt.Println("Invalid day")
}
}
Type Switch
A Type Switch is used to branch on the type of an interface value, rather than its value. This is particularly useful when dealing with variables of unknown types.
Syntax
switch var := interfaceValue.(type) {
case type1:
// Code block
case type2:
// Code block
default:
// Code block
}
Example
In this example, we use the same day
variable but wrapped in an interface{}
to demonstrate the type switch.
Go
package main
import "fmt"
func main() {
var day interface{} = 4
switch v := day.(type) {
case int:
switch v {
case 1:
fmt.Println("Monday")
case 2:
fmt.Println("Tuesday")
case 3:
fmt.Println("Wednesday")
case 4:
fmt.Println("Thursday")
case 5:
fmt.Println("Friday")
default:
fmt.Println("Invalid day")
}
default:
fmt.Printf("Unknown type: %T\n", v)
}
}
If and Switch Statement in Go Programming Language
Explore
Go Tutorial
3 min read
Overview
Fundamentals
Control Statements
Functions & Methods
Structure
Arrays
Slices
Strings
Pointers