go - 一个案件可以执行多个案件吗?
问题描述
我想为 case 执行 case'b'
和'c'
case 'a'
:
foo := 'a'
switch foo {
case 'a':
// execute both case 'b' and case 'c'
case 'b':
// only execute case 'b'
case 'c':
// only execute case 'c'
}
我试图将 case'a'
转换为 case 'b'
,然后将 case'b'
转换为 case 'c'
if foo == 'a'
,但是不允许有条件的 fallthrough 语句,它们会抛出"fallthrough statement out of place"
错误。
这个问题可以通过将代码从 case'b'
和 case复制'c'
到 case'a'
中来解决,也可以通过使用 switch 语句之外的其他条件分支方法来解决,但是我想知道是否有使用 switch 语句而不复制来解决这个问题案例之间的代码以实现类似的结果。
解决方案
几个选项:
if foo == 'a' || foo == 'b' {
// case 'b'
}
if foo == 'a' || foo == 'c' {
// case 'c'
}
switch foo {
case 'a':
b()
c()
case 'b':
b()
case 'c':
c()
}
func b() { /* ... */ }
func c() { /* ... */ }
推荐阅读
- matplotlib - 使用 jupyter notebook 找不到 matplotlib 模块
- javascript - Javascript 中的 Google Cloud 函数在函数完成之前完成
- reactjs - 如何从我的 ReactJS 应用程序连接到 RabbitMQ?
- java - 我可以输出一个没有过多空格的数组吗?
- azure-data-factory - 如何使用 ADF 检查同一源中的重复项
- c - GCC 似乎将 C *.d 依赖文件与 D 语言源代码混淆了
- python - 将上三角矩阵转换为对称矩阵的快速方法
- woocommerce - Woocommerce - 如何在所选支付网关的“我的帐户订单”页面上隐藏“支付按钮 url”。示例:仅显示 paypal 的付款按钮
- if-statement - 将单元格的逻辑插入到 Google 表格中另一个单元格的逻辑中
- jquery - 为什么没有应用这个 jquery .css 样式?