Showing posts with label statement. Show all posts
Showing posts with label statement. Show all posts

Friday, August 25, 2017

Switch Case Statement in Golang

Switch Case Statement in Golang


PROGRAM:

package main

import "fmt"

func main() {
/* type has to match with case, there is no need break like C++ or JAVA has*/
pc1 := "ibm"
switch pc1 {
case "dell":
fmt.Println("DELL Computer")
case "ibm":
fmt.Println("IBM Computer")
case "hp":
fmt.Println("HP Computer")
default:
fmt.Println("APPLE Computer")
}

/* initialisation int the same statement */
switch pc2 := "hp"; pc2 {
case "dell":
fmt.Println("DELL Computer")
case "ibm":
fmt.Println("IBM Computer")
case "hp":
fmt.Println("HP Computer")
default:
fmt.Println("APPLE Computer")
}

/* fallthrough keyword helps to execute block below the case */
switch pc3 := "ibm"; pc3 {
case "dell":
fmt.Println("DELL Computer")
case "ibm":
fmt.Print("IBM & ")
fallthrough
case "hp":
fmt.Println("HP Computer")
default:
fmt.Println("APPLE Computer")
}
}

OUTPUT:
IBM Computer
HP Computer
IBM & HP Computer

download file now

Read more »