草庐IT

go - 执行多个 switch case

coder 2023-07-02 原文

我有以下代码:

package main

import (
    "fmt"
)

func main() {

    switch num := 75; { //num is not a constant
    case num < 50:
        fmt.Printf("%d is lesser than 50\n", num)   
    case num < 100:
        fmt.Printf("%d is lesser than 100\n", num)  
    case num < 60:
        fmt.Printf("%d is lesser than 60", num)
    case num < 200:
        fmt.Printf("%d is lesser than 200", num)
    }
}

如果我想执行下一个案例,我可以使用 fallthrough,但它不会根据案例检查条件。我需要检查条件:我想像往常一样继续 switch case,即使它遇到了一个 case。

我也想用 fallthrough 检查下一个案例条件,有什么办法可以做到吗?

最佳答案

简短回答:,您不能使用fallthrough 检查后续的case 条件,因为fallthrough 是无条件的并且强制执行下一个案例。这就是它的目的。


长答案:你仍然可以使用fallthrough:如果你仔细观察这种情况,你只需要以一种有意义的方式重新排序你的案例。请注意,这对于一般情况而言并非如此。

switch num := 75; {
case num < 50:
    fmt.Printf("%d is less than 50\n", num)
    fallthrough // Any number less than 50 will also be less than 60
case num < 60:
    fmt.Printf("%d is less than 60\n", num)
    fallthrough // Any number less than 60 will also be less than 100
case num < 100:
    fmt.Printf("%d is less than 100\n", num)      
    fallthrough // Any number less than 100 will also be less than 200
case num < 200:
    fmt.Printf("%d is less than 200\n", num)
}

这样您就可以安全地使用fallthrough,因为由于条件的顺序是正确的,您已经知道如果num 通过其中之一,那么它显然也会通过每个后续条件。

此外,如果您实际上不想简单地fallthrough到下一个案例,而是想执行另一个案例,您可以添加一个带标签的案例并使用goto 直接跳到它。

switch num := 75; {
firstCase:
    fallthrough
case num < 50:
    fmt.Println("something something\n")
case num < 100:
    fmt.Printf("%d is less than 100\n", num)  
    goto firstCase
}

查看 Go GitHub wiki here获取更多信息。


顺便说一下,如果您正在使用 switch 语句并且需要强制执行一些不自然的案例处理,那么您可能做错了整件事:考虑使用另一种方法,例如一些嵌套的 if 语句。

关于go - 执行多个 switch case,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50984814/

有关go - 执行多个 switch case的更多相关文章

  1. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  2. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  3. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  4. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  5. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  6. ruby - Chef 执行非顺序配方 - 2

    我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul

  7. ruby-on-rails - 在 ruby​​ .gemspec 文件中,如何指定依赖项的多个版本? - 2

    我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这

  8. ruby - 为什么 Ruby 的 each 迭代器先执行? - 2

    我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试

  9. ruby - 使用多个数组创建计数 - 2

    我正在尝试按0-9和a-z的顺序创建数字和字母列表。我有一组值value_array=['0','1','2','3','4','5','6','7','8','9','a','b','光盘','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','','u','v','w','x','y','z']和一个组合列表的数组,按顺序,这些数字可以产生x个字符,比方说三个list_array=[]和一个当前字母和数字组合的数组(在将它插入列表数组之前我会把它变成一个字符串,]current_combo['0','0','0']

  10. ruby-on-rails - before_filter 运行多个方法 - 2

    是否有可能:before_filter:authenticate_user!||:authenticate_admin! 最佳答案 before_filter:do_authenticationdefdo_authenticationauthenticate_user!||authenticate_admin!end 关于ruby-on-rails-before_filter运行多个方法,我们在StackOverflow上找到一个类似的问题: https://

随机推荐