Switch Statement in Golang

Create new folder named src. In src folder, create new file named main.go as below:

package main

import "fmt"

func main() {

	fmt.Println("Demo 1")
	Demo1()

	fmt.Println("Demo 2")
	Demo2()

	fmt.Println("Demo 3")
	Demo3()

}

func Demo1() {
	var finger int = 3
	switch finger {
		case 1:
			fmt.Println("Thumb")
		case 2:
			fmt.Println("Index")
		case 3:
			fmt.Println("Middle")
		case 4:
			fmt.Println("Ring")
		case 5:
			fmt.Println("Pinky")
		default:
			fmt.Println("Invalid")
	}
}

func Demo2() {
	a := 2
	switch a {
		case 1, 2, 3:
			fmt.Println("a = 1, 2, 3")
		case 4, 5, 6:
			fmt.Println("a = 4, 5, 6")
		default:
			fmt.Println("Invalid")
	}
}

func Demo3() {
	a := 2
	switch {
		case a >= 1 && a <= 10:
			fmt.Println("a >= 1 and a <= 10")
		case a >= 10 && a <= 20:
			fmt.Println("a >= 10 and a <= 20")
		default:
			fmt.Println("Invalid")
	}
}




Open Terminal windows in Visual Studio Code and run command line: go run main.go

Demo 1
Middle

Demo 2
a = 1, 2, 3

Demo 3
a >= 1 and a <= 10