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
package main
import "fmt"
func main() {
value := 4
switch value {
case 4 :
fmt.Println(true)
case 4 :
fmt.Println(true)
}
}
|
Output:
./prog.go:15:10: duplicate case 4 in switch
previous case at ./prog.go:13:10
Example 2:
Go
package main
import "fmt"
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" )
}
}
|
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
Last Updated :
07 Jul, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...