Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go开关/箱子是否有故障?_Go_Switch Statement - Fatal编程技术网

Go开关/箱子是否有故障?

Go开关/箱子是否有故障?,go,switch-statement,Go,Switch Statement,当您到达一个Go案例的结尾时会发生什么情况,它是否会进入下一个案例,或者假设大多数应用程序都不希望失败?否,Go开关语句在默认情况下不会失败。如果确实希望它出错,则必须显式使用“fallthrough”语句。从: 在case或default子句中,最后一个非空语句可以是 (可能标记为“fallthrough”语句以指示该控件 应该从本条款末尾到 下一条。否则,控制流到“开关”的末端 陈述“fallthrough”语句可能会作为最后一个语句出现 除表达式开关的最后一个子句外的所有语句 例如(对不起

当您到达一个Go案例的结尾时会发生什么情况,它是否会进入下一个案例,或者假设大多数应用程序都不希望失败?

否,Go开关语句在默认情况下不会失败。如果确实希望它出错,则必须显式使用“fallthrough”语句。从:

在case或default子句中,最后一个非空语句可以是 (可能标记为“fallthrough”语句以指示该控件 应该从本条款末尾到 下一条。否则,控制流到“开关”的末端 陈述“fallthrough”语句可能会作为最后一个语句出现 除表达式开关的最后一个子句外的所有语句

例如(对不起,我想不出一个真实的例子):


中断保留为默认值,但不是故障排除。如果您想进入下一个匹配案例,您应该明确提到故障排除

switch choice {
case "optionone":
    // some instructions 
    fallthrough // control will not come out from this case but will go to next case.
case "optiontwo":
   // some instructions 
default: 
   return 
}

不可以,但是您可以通过使用
fallthrough
关键字note显式指定;“漏洞”必须是本案的最后一件事;尽管您可以使用Not“for a match”来解决这个问题:fallthrough将执行下一个案例的主体,而不检查下一个案例的匹配!
switch choice {
case "optionone":
    // some instructions 
    fallthrough // control will not come out from this case but will go to next case.
case "optiontwo":
   // some instructions 
default: 
   return 
}