Go switch Statement

In Go, the switch statement is used to perform different actions based on the value of an expression. It provides a concise way to write multiple if-else statements for the same variable or expression. Here’s the syntax of the switch statement in Go:

switch expression {
case value1:
    // Code to be executed if expression matches value1
case value2:
    // Code to be executed if expression matches value2
...
default:
    // Code to be executed if expression doesn't match any case
}

Here are a few examples of using the switch statement in Go:

  1. Switch statement with integer values:
   day := 3
   switch day {
   case 1:
       fmt.Println("Monday")
   case 2:
       fmt.Println("Tuesday")
   case 3:
       fmt.Println("Wednesday")
   default:
       fmt.Println("Other day")
   }
  1. Switch statement with string values:
   fruit := "apple"
   switch fruit {
   case "apple":
       fmt.Println("It's an apple")
   case "banana":
       fmt.Println("It's a banana")
   default:
       fmt.Println("Unknown fruit")
   }
  1. Switch statement with expressions:
   age := 20
   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")
   }

The switch statement evaluates the expression and compares it against the specified values or conditions. If a match is found, the corresponding block of code is executed. If no match is found, the code inside the default case (if present) is executed. If no match is found and there is no default case, no code is executed.

The switch statement in Go provides a clean and concise way to handle multiple cases based on the value of an expression. It offers a readable and efficient alternative to multiple if-else statements.

Go Single-Case switch Example

In Go, a single-case switch statement can be used when you have a single value to compare against. It provides a concise way to handle a specific case without the need for multiple cases. Here’s an example of a single-case switch statement in Go:

fruit := "apple"

switch fruit {
case "apple":
    fmt.Println("It's an apple")
}

In the above example, the switch statement compares the value of the fruit variable against the case “apple”. If the value matches, the code inside the case block is executed, which in this case, prints “It’s an apple” to the console.

Single-case switch statements are useful when you have a specific value to check against and only need to perform an action for that particular case. They provide a clean and concise way to handle such scenarios in your Go programs.

Go default switch Statement Keyword.

In Go, the default keyword is used in the switch statement as a fallback option. It represents the default case that is executed when none of the other cases match the value of the expression. The default case is optional and is typically used to handle situations where none of the expected cases are satisfied. Here’s an example of using the default case in a switch statement:

fruit := "orange"

switch fruit {
case "apple":
    fmt.Println("It's an apple")
case "banana":
    fmt.Println("It's a banana")
default:
    fmt.Println("Unknown fruit")
}

In the above example, if the value of fruit is neither “apple” nor “banana”, the default case will be executed, and the message “Unknown fruit” will be printed.

It’s important to note that the default case doesn’t have to be the last case in the switch statement. It can be placed anywhere in the switch block, but it’s typically used as the last case to handle unexpected or unmatched values.

The default case provides a way to handle scenarios where none of the expected cases are satisfied. It allows you to provide a fallback action or handle default behavior when no specific match is found in the switch statement.

Multiple expressions in case.

In Go, you can use multiple expressions in a single case of a switch statement. This allows you to match multiple values or conditions in a single case. To use multiple expressions in a case, you can separate them with commas. Here’s an example:

age := 25
grade := "B"

switch {
case age >= 18, grade == "A":
    fmt.Println("Qualified")
case age < 18, grade == "F":
    fmt.Println("Not qualified")
default:
    fmt.Println("Unknown")
}

In the above example, the switch statement doesn’t have an expression. Instead, it checks multiple conditions in each case. The first case checks if age is greater than or equal to 18 or if grade is “A”. If either of these conditions is true, it prints “Qualified”. The second case checks if age is less than 18 or if grade is “F”, and if any of these conditions is true, it prints “Not qualified”. If none of the cases match, the default case is executed, printing “Unknown”.

Using multiple expressions in a case allows you to handle different scenarios with a single case. It provides flexibility in matching multiple values or conditions and executing the corresponding code block.

The Multi-case switch Statement In Go

In Go, you can use a multi-case switch statement to handle multiple cases with the same code block. It allows you to execute the same set of statements for multiple case values without repeating the code. Here’s an example of a multi-case switch statement in Go:

day := "Saturday"

switch day {
case "Saturday", "Sunday":
    fmt.Println("It's the weekend")
case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday":
    fmt.Println("It's a weekday")
default:
    fmt.Println("Invalid day")
}

In the above example, the switch statement checks the value of the day variable against multiple case values. If the day is “Saturday” or “Sunday”, it executes the code block inside the first case, printing “It’s the weekend”. If the day is any of the weekdays (“Monday” to “Friday”), it executes the code block inside the second case, printing “It’s a weekday”. If none of the cases match, the code block inside the default case is executed, printing “Invalid day”.

Using a multi-case switch statement helps you write more concise code when you want to handle multiple cases with the same logic. It eliminates the need to repeat the code block for each case and improves code readability and maintainability.