Go switch
Go switch Statement
The switch statement in Go provides an efficient way to handle multiple conditional branches. It is an alternative to a long sequence of if-else statements and improves code readability.
Syntax of switch in Go
switch expression { case value1: // Execute this code if the expression matches value1. case value2: // Execute this code if the expression matches value2. default: // Execute this code when no cases match. }
How the switch Works
- The expression is evaluated once and compared against each case.
- If a match is found, the associated code block runs.
- If no match is found, the default block (if available) executes.
- Unlike other languages like C or Java, Go’s switch does not require a break statement to exit a case—it happens automatically.
Types of switch Statements in Go
1. Simple switch Statement
A straightforward switch example where a variable's value determines which block executes.
Example:
package main import "fmt" func main() { day := "Monday" switch day { case "Monday": fmt.Println("Start of the workweek!") case "Friday": fmt.Println("Weekend is near!") default: fmt.Println("A regular day.") } }
Output:
Start of the workweek!
Explanation: Since day is "Monday", the first case block runs.
2. switch Without an Expression (Tagless switch)
Go allows switch statements without an explicit expression, acting like a cleaner if-else ladder.
Example:
package main import "fmt" func main() { age := 25 switch { case age < 18: fmt.Println("You are a minor.") case age >= 18 && age < 60: fmt.Println("You are an adult.") default: fmt.Println("You are a senior citizen.") } }
Output:
You are an adult.
Explanation: The first matching condition executes (age >= 18 && age < 60).
3. Multiple Values in a case Statement
A case can contain multiple values separated by commas.
Example:
package main import "fmt" func main() { char := 'a' switch char { case 'a', 'e', 'i', 'o', 'u': fmt.Println("Vowel") default: fmt.Println("Consonant") } }
Output:
Vowel
Explanation: Since char is 'a', which is in the list ('a', 'e', 'i', 'o', 'u'), the first case runs.
4. Using fallthrough
By default, the switch statement does not continue to the next case automatically. However, fallthrough can be used to continue execution.
Example:
package main import "fmt" func main() { num := 2 switch num { case 1: fmt.Println("One") case 2: fmt.Println("Two") fallthrough case 3: fmt.Println("Three") default: fmt.Println("Other number") } }
Output:
Two Three
Explanation: The case 2 matches, prints "Two", and because of fallthrough, the next case (3) executes as well.
5. Type switch (Switching on Interface Types)
A type switch determines the underlying type of an interface.
Example:
package main import "fmt" func checkType(value interface{}) { switch v := value.(type) { case int: fmt.Println("Integer:", v) case string: fmt.Println("String:", v) case bool: fmt.Println("Boolean:", v) default: fmt.Println("Unknown type") } } func main() { checkType(10) checkType("Hello") checkType(true) }
Output:
Integer: 10 String: Hello Boolean: true
Explanation: The function checkType determines and prints the type of the given value.
Conclusion
- Go’s switch is clean and efficient, avoiding repetitive if-else chains.
- No break required, as Go automatically exits after a match.
- switch without an expression behaves like an if-else statement.
- Multiple values can be used in a single case for compact conditions.
- fallthrough allows execution to continue to the next case.
- type switch helps determine dynamic types in interfaces.