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 example we can see that by using switch cases and assuming variable as a string type we can make use of switch cases.
package main
import "fmt"
func main() {
day := "Tue"
switch {
case day == "Mon" :
fmt.Println( "Monday" )
case day == "Tue" :
fmt.Println( "Tuesday" )
case day == "Wed" :
fmt.Println( "Wednesday" )
}
}
|
Output :
Tuesday
Example 2:
package main
import "fmt"
func main() {
gfg := "Geek"
switch {
case gfg == "Geek" :
fmt.Println( "Geek" )
case gfg == "For" :
fmt.Println( "For" )
case gfg == "Geeks" :
fmt.Println( "Geeks" )
}
}
|
Output:
Geek
Last Updated :
04 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...