
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implement Switch Statement on Strings in Go
A switch statement in Golang is a control flow statement that enables you to run one block of Example: in response to various situations. Similar to if-else expressions, it can test several conditions more quickly. In this article, we will see how switch statement is implemented on strings in Go.
Method 1: Using default case as fallback option
In this method, if a match is detected, this program will verify the input variable, compare it to the cases, and execute the associated block of Example:; otherwise, it will run the default case block. Let's see through the Example: and algorithm how it's executed.
Algorithm
Step 1 ? Create a package main and declare fmt(format package) package in the program where main produces executable examples and fmt helps in formatting input and output.
Step 2 ? Create a function main and in that function create a variable called input value and initialize it with the string "option2".
Step 3 ? Use the variable input_value after the switch keyword.
Step 4 ? To specify the possibilities for the switch statement, use the case keyword followed by a string value.
Step 5 ? If the variable input_value matches the supplied case option, write the Example: that should be run.
Step 6 ? To handle any input that doesn't match one of the case options, use the default case as a fallback option.
Step 7 ? To print the chosen choice, use fmt.Println() function where ln means new line.
Example
In this program we will learn how to execute switch statement on strings using default case a fallback option
package main import "fmt" func main() { fmt.Println("The switch statement is executed like:") input_value := "option2" //create an input value switch input_value { case "option1": fmt.Println("Option 1 selected") case "option2": fmt.Println("Option 2 selected") case "option3": fmt.Println("Option 3 selected") default: fmt.Println("Invalid option selected") //set a default case } }
Output
The switch statement is executed like: Option 2 selected
Method 2: Using fallthrough keyword
In this method,the fallthrough keyword is used to forward execution to the next case block in this example, if the input is "option2," the program will run the Example: block for, "option2" selected, and "option3" selected.
Algorithm
Step 1 ? Create a package main and declare fmt(format package) package in the program where main produces executable examples and fmt helps in formatting input and output.
Step 2 ? As per the above program repeat till step4 here and add fallthrough in option1,2 and 3.
Step 3 ? Use the default case as invalid option selected.
Step 4 ? With the help of fallthrough the execution of next block will continue.
Step 5 ? The output will be printed on the console using fmt.Println() function where ln means new line.
Example
In this example, we will see how we can execute switch statement on strings using fall through keyword.
package main import "fmt" func main() { input_value := "option2" //create an input value fmt.Println("The switch statement is executed like:") switch input_value { case "option1": fmt.Println("Option 1 selected") fallthrough case "option2": fmt.Println("Option 2 selected") fallthrough case "option3": fmt.Println("Option 3 selected") default: fmt.Println("Invalid option selected") //set default case } }
Output
The switch statement is executed like: Option 2 selected Option 3 selected
Method 3: Using Maps
In this method, if a match is found, the program will run the associated function; otherwise, the default case block will be run. This program will verify the input variable and compare it with the keys in the map.
Algorithm
Step 1 ? Create a package main and declare fmt(format package) package in the program where main produces executable examples and fmt helps in formatting input and output.
Step 2 ? Create a main function and in that function create a variable called input_value and initialize it with the string "option2".
Step 3 ? Make a map with the name options where the values are the matching Example: blocks and the keys are the string choices.
Step 4 ? To determine whether the input variable is used as a key in the options map, use an if statement.
Step 5 ? Call the function saved as the key's value to run the matching Example: block if the key is present.
Step 6 ? Run a default case block that prints "Invalid choice selected" if the key is missing.
Example
In this program we will learn how to implement switch statement on strings with the help of maps.
package main import "fmt" func main() { input_value := "option2" //create input value fmt.Println("The switch statement is executed like:") options := map[string]func(){ //create a map "option1": func() { fmt.Println("Option 1 selected") }, "option2": func() { fmt.Println("Option 2 selected") }, "option3": func() { fmt.Println("Option 3 selected") }, } if fn, ok := options[input_value]; ok { fn() } else { fmt.Println("Invalid option selected") } }
Output
The switch statement is executed like: Option 2 selected
Conclusion
We executed the program of implementing switch statement on strings using three examples. In the first example we used simple switch case, in the second example we used fallthrough keyword and in the third example we used the map.