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
// 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)
}
} |
Output:
./prog.go:15:10: duplicate case 4 in switch previous case at ./prog.go:13:10
Example 2:
Go
// 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" )
}
} |
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
Article Tags :