Switch Statement in Golang

  • date 3rd September, 2022 |
  • by Prwatech |
  • 0 Comments

How To Write Switch Statements in Go

 

In Go, switch statements provide a concise and flexible way to evaluate expressions against multiple possible cases and execute corresponding blocks of code based on matching conditions. The syntax of a switch statement in Go is similar to other languages, but it offers several unique features.

One notable feature of Go’s switch statement is that it does not require the use of explicit “break” statements after each case. Instead, Go automatically breaks out of the switch block after executing a matching case, preventing fall-through behavior by default.

Go’s switch statements can evaluate expressions of various types, including integers, strings, and other types, allowing for versatile conditional logic. Additionally, Go allows multiple expressions in a single case, providing flexibility in matching conditions.

Switch statements in Go can also include a default case, which is executed when none of the preceding cases match the expression’s value.

Go language has two types of switch statements:

Syntax:

switch expression {
case x:
   // code block
case y:
   // code block
case z:
...
default:
   // code block
}
  1. Expression Switch (Single case)
  2. Type Switch (Multi case)

 

  1. Single-case

1. The example below uses a number to return different text

package main
import "fmt"
func main() {
    fmt.Print("Enter the No. :")
    var I int
    fmt.Scanln(&I)
    switch I {
    case 1:
        fmt.Print("The value is 10 ")
    case 2:
        fmt.Print("The value is 20 ")
    case 3:
        fmt.Print("The value is 30 ")
    case 4:
        fmt.Print("The value is 40 ")
    case 5:
    default:
        fmt.Print("Incorrect input ")
}
}

Output:

PS C:\GO_Language\Case> go run case1.go
Enter No. :4
The value is 40 
PS C:\GO_Language\Case> go run case1.go
Enter No. :2
The value is 20 
PS C:\GO_Language\Case> go run case1.go
Enter No. :1
The value is 10 
PS C:\GO_Language\Case> go run case1.go
Enter No. :6
It is not in bound

2.  The example below uses a weekday number to calculate the weekday name:

package main

import (
    "fmt"
)

func main() {
    fmt.Print("Enter Day No. :")
    var i int
    fmt.Scanln(&i)

    switch i {
    case 1:
        fmt.Println("Monday")
    case 2:
        fmt.Println("Tuesday")
    case 3:
        fmt.Println("Wednesday")
    case 4:
        fmt.Println("Thursday")
    case 5:
        fmt.Println("Friday")
    case 6:
        fmt.Println("Saturday")
    case 7:
        fmt.Println("Sunday")
    default:
        fmt.Print("It is not in bound")

    }
} 

Output :

PS C:\GO_Language\Case> go run case3.go
Enter Day No. :1
Monday
PS C:\GO_Language\Case> go run case3.go 
Enter Day No. :3
Wednesday
PS C:\GO_Language\Case> go run case3.go
Enter Day No. :5
Friday
PS C:\GO_Language\Case> go run case3.go
Enter Day No. :6
Saturday

           

 

 

2 . Multi-case

    1. The example below uses number to return different text:
      package main
      import "fmt"
      func main() {
          var value string = "five"
          switch value {
          case "one":
              fmt.Println("C#")
          case "two", "three":
              fmt.Println("Go")
          case "four", "five", "six":
              fmt.Println("java")
          }
      }
      

      Output :

      PS C:\GO_Language\Case> go run case2.go
      java
      

      How To Write Switch Statements in Go

0
0

Quick Support

image image