交换机的情况下,必须有一个独特的价值。当针对整个开关检查重复值时。出现“重复案例”错误。让我们在示例的帮助下讨论重复案例错误:
示例 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)
}
}
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")
}
}
输出:
./prog.go:15:10: duplicate case 4 in switch
previous case at ./prog.go:13:10
示例 2:
去
// 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")
}
}
输出:
./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