Open In App

Golang program that uses switch, multiple value cases

Last Updated : 02 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Switch statement is a multiway branching which provides an alternative way too lengthy if-else comparisons. It selects a single block to be executed from a listing of multiple blocks on the basis of the value of an expression or state of a single variable. A switch statement using multiple value cases correspond to using more than one value in a single case. This is achieved by separating the multiple values in the case with a comma. 

Example 1: 

Go




// Golang program to illustrate the
// use of switch with multiple value cases
package main
 
import (
    "fmt"
)
 
func main() {
 
    // string to input month from user
    var month string
    fmt.Scanln(&month)
 
    // switch case for predicting
    // seasons for month entered
     
    // each switch case has more
    // than one values
    switch month {
        case "january", "december":
            fmt.Println("Winter.")
        case "february", "march":
            fmt.Println("Spring.")
        case "april", "may", "june":
            fmt.Println("Summer.")
        case "july", "august":
            fmt.Println("Monsoon.")
        case "september", "november":
            fmt.Println("Autumn.")
    }
}


Input : january
Output : Winter.

Input : september
Output : Autumn.

Rather than making different individual cases for months having the same season, we clubbed different months with the same output. This saves us to write redundant pieces of code. 

Example 2: 

Go




// Golang program to illustrate the
// use of switch with multiple value cases
package main
 
import (
    "fmt"
)
 
func main() {
 
    // integer to input number from
    // user (only 1-10)
    var number int
    fmt.Scanln(&number)
 
    // switch case for predicting
    // whether the number is even or odd
     
    // each switch case has more
    // than one values
    switch number {
        case 2, 4, 6, 8, 10:
            fmt.Println("You entered an even number.")
        case 1, 3, 5, 7, 9:
                fmt.Println("You entered an odd number.")
    }
}


Input : 6
Output : You entered an even number.

Input : 5
Output : You entered an odd number.

Instead of writing 10 different cases to check whether the entered number is even or not, we could simply do the same in 2 switch cases using multiple case values. 

Example 3: 

Go




// Golang program to illustrate the
// use of switch with multiple value cases
package main
 
import (
    "fmt"
)
 
func main() {
 
    // character input (a-z or A-Z)
    var alphabet string
    fmt.Scanln(&alphabet)
 
    // switch case for predicting
    // whether the character is
    // uppercase or lowercase
     
    // each switch case has more
    // than one values
    switch alphabet {
        case "a", "b", "c", "d", "e", "f", "g", "h", "i",
        "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
        "u", "v", "w", "x", "y", "z":
            fmt.Println("Lowercase alphabet character.")
             
        case "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
        "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
        "U", "V", "W", "X", "Y", "Z":
            fmt.Println("Uppercase alphabet character.")
    }
}


Input : g
Output : Lowercase alphabet character.

Input : F
Output : Uppercase alphabet character.


Similar Reads

Golang Program that uses String Switch
With the help of switch case we can implement the functionality of as many if statements. In Golang, switch cases can work with strings, list of variables including integer values as well as floating values. Syntax: switch optstatement; optexpression{case expression1: Statement..case expression2: Statement..…default: Statement..}Example 1: In this
2 min read
Golang Program that Uses Multiple Return Values
In Golang, we can return multiple values at a time from a single function. Multiple return values can be achieved by changing the return type of the function in the function signature. Syntax : func value( ) ( int , int ) { return 1 , 0 ; } The (int, int) in this function signature explains that the return type is two integers. Thus we have to use
2 min read
Golang Program that switch on floating-point numbers
With the help of switch case we can implement the functionality of as many if statements. In Golang, switch cases can work with strings, list of variables including integer values as well as floating values. Syntax: switch optstatement; optexpression{ case expression1: Statement.. case expression2: Statement.. ... default: Statement.. } Example 1:
2 min read
Golang Program to Show the Duplicate Case Error in Switch Statement
A switch's case must have a unique value. When repeated value is checked against the entire switch. The "duplicate case" error is arised. Let us discuss duplicate case error with the help of the examples: Example 1: C/C++ Code // Golang program that causes // duplicate case error package main import "fmt" // Main function func main() { va
1 min read
Golang program that uses fallthrough keyword
With the help of fallthrough statement, we can use to transfer the program control just after the statement is executed in the switch cases even if the expression does not match. Normally, control will come out of the statement of switch just after the execution of first line after match. Don't put the fallthrough in the last statement of switch ca
2 min read
Golang program that uses structs as map keys
A map in Golang is a collection of unordered pairs of key-value. It is widely used because it provides fast lookups and values that can retrieve, update or delete with the help of keys. Syntax: map[Key_Type]Value_Type{} Example: var sample map[string]int Here the sample is a map that has a string as key and int type as the value. In maps, most of t
5 min read
Golang program that uses func with two arguments
Functions may use certain parameters to receive values and perform the task required accordingly. The syntax for a function with two arguments is: func function_name( [parameter_1 parameter_2] ) [return_types] { body of the function } Example 1: Without any return type // Golang program that uses func with // two arguments without any return type p
1 min read
Golang program that uses defer keyword
The main use of the defer keyword in go is that it is used to clean up the resources used by opened files, network connections, database handles, etc. This helps to reduce the chances of error by keeping the call to close function/files nearer to call to open statements. The statement that is followed by defer keyword is placed onto a stack and in
2 min read
Golang Program that Uses Named Return Values and Defaults
Golang functions have special functionality that allows them to provide the name to the return values. These named return values can be used as arguments or variables. The named return values also use the default values for the data types like 0 for int type etc. To understand this concept let's take an example: // Golang Program that Uses Named //
1 min read
Golang Program that uses func as Local Variable
Function performs a specific task, section of code that defined once can be reused. Functions are used to make your code easier to understand by breaking it into small and understandable tasks.Functions are also known as method, sub-routine, or procedure. General form of a function definition in Go programming language is given below- func function
3 min read
Article Tags :