Golang Program to Show the Duplicate Case Error in Switch Statement

A switch’s case must have a unique value. When repeated value is checked against the entire switch. The “duplicate case” error is arised. Let us discuss duplicate case error with the help of the examples:

Example 1:  

Go

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program that causes 
// duplicate case error
   
package main
  
import "fmt"
  
// Main function
func main() {
    value := 4
  
    // Duplicate cases are not allowed.
    switch value {
    case 4:
        fmt.Println(true)
    case 4:
        fmt.Println(true)
    }
}

chevron_right


Output:

./prog.go:15:10: duplicate case 4 in switch
    previous case at ./prog.go:13:10

Example 2:

Go

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program that causes 
// duplicate case error
   
package main
  
import "fmt"
  
// Main function
func main() {
    i := 1
    switch i {
        case 0, 1:
            fmt.Println("GeeksForGeeks")
            fallthrough
        case 0:
            fmt.Println("Geeks_0")
        case 1:
            fmt.Println("Geeks_1")
        default:
            fmt.Println("Number_of_Users")
    }
}

chevron_right


Output:

./prog.go:15:8: duplicate case 0 in switch
    previous case at ./prog.go:12:8
./prog.go:17:8: duplicate case 1 in switch
    previous case at ./prog.go:12:11



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : Akanksha_Rai

Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.