Go programming Language switch case

Go Programming Language switch case

switch case is another one of the approach to build the decision in go programming.

Syntax:
switch option {
	case option1:
		// task1
	case option2:
		//task2
	default:
		//task n
}

Go Language switch case Example

Below code illustrates the switch case decision blocks in Go programming.

package main                                                               	 
                                                                           	 
import "fmt"                                                               	 
                                                                           	 
func main() {                                                              	 
                                                                           	 
	// declare variables                                                   	 
	var option int                                                         	 
                                                                           	 
	fmt.Print("Enter option: ")                                            	 
	fmt.Scanf("%d", &option)                                               	 
                                                                           	 
	fmt.Println("Go switch case")                                          	 
	switch option {                                                        	 
    	case 1:                                                            	 
        	fmt.Println("Option 1 is entered!")                            	 
    	case 2:                                                            	 
        	fmt.Println("Option 2 is entered!")                            	 
    	case 5:                                                            	 
        	fmt.Println("Option 5 is entered!")                            	 
    	default:                                                           	 
        	fmt.Println("Unknown option is entered!")                      	 
	}                                                                      	 
} 
Output:
$ go build switch-case.go
$ ./switch-case
Enter option: 2
Go switch case
Option 2 is entered!
$ ./switch-case
Enter option: 5
Go switch case
Option 5 is entered!
$ ./switch-case
Enter option: 3
Go switch case
Unknown option is entered!