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 floating type we can make use of switch cases. Some of the programming languages don’t allow us to use float values but Golang allows us.
// Golang Program that switch // on floating-point numbers package main // Here "fmt" is formatted IO which // is same as C’s printf and scanf. import "fmt" // Main function func main() { val := 1.1 // Use switch on the day variable. switch { case val == 1.2: fmt.Println("One Point Two") case val == 1.3: fmt.Println("One Point Three") case val == 1.1: fmt.Println("One Point One") } } |
Output :
One Point One
Example 2:
// Golang Program that switch // on floating-point numbers package main // Here "fmt" is formatted IO which // is same as C’s printf and scanf. import "fmt" // Main function func main() { gfg := 4.5 // Use switch on the day variable. switch { case gfg == 1.2: fmt.Println("Geek") case gfg == 4.5: fmt.Println("For") case gfg == 5.5: fmt.Println("Geeks") } } |
Output :
For
Recommended Posts:
- Golang Program that uses String Switch
- Golang program that uses switch, multiple value cases
- Golang Program to Show the Duplicate Case Error in Switch Statement
- Switch Case with Break in For Loop in Golang
- How to use a switch case 'or' in PHP?
- Switch Statement in Go
- How to optimize the switch statement in JavaScript ?
- Switch case in R
- What is ISL(Inter-Switch Link)?
- What is SMPS(Switch Mode Power Supply)?
- Decision Making in R Programming - if, if-else, if-else-if ladder, nested if-else, and switch
- How to add bootstrap toggle-switch using JavaScript ?
- Generating Random Numbers in Golang
- Finding Maximum of Two Numbers in Golang
- Finding Minimum of Two Numbers in Golang
- Golang program that uses fallthrough keyword
- Golang program that uses structs as map keys
- Golang Program to Count Trailing Zeros in Factorial of a Number
- Golang Program to Find Largest Element in an Array
- Golang program that uses func with variable argument list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

