duanlou2917 2018-07-09 11:35
浏览 62
已采纳

嵌套开关在golang中合法吗? [关闭]

I hope golang syntax can be time resistant (meaning if I write my code now, 10 years time the syntax is still valid)

            switch strings.ToLower(firstCommand) {
            default:
                    fmt.Println("Default 1st Nest")

            case "c":
                    fmt.Println("C CREATE")
                    fallthrough
            case "u":
                    fmt.Println("U UPDATE")
                    fallthrough
            case "d":
                    fmt.Println("D DELETE")
                    dosomething()

                    switch strings.ToLower(secondCommand) {
                    default:
                    fmt.Println("Default 2nd Nest")

                    case "a":
                    donothing()

                    }
           }

is the above code syntax legit? I mean, will it stand the test of time? Otherwise, what is a better way to write this?

  • 写回答

1条回答 默认 最新

  • dongwen2794 2018-07-09 12:20
    关注

    Read the The Go Programming Language Specification, the Go 1 compatibility guarantees, and the planning for Go 2.


    The Go Programming Language Specification

    Blocks

    Each clause in a "switch" or "select" statement acts as an implicit block.

    Switch statements

    "Switch" statements provide multi-way execution. An expression or type specifier is compared to the "cases" inside the "switch" to determine which branch to execute.


    Go 1 and the Future of Go Programs

    It is intended that programs written to the Go 1 specification will continue to compile and run correctly, unchanged, over the lifetime of that specification. At some indefinite point, a Go 2 specification may arise, but until that time, Go programs that work today should continue to work even as future "point" releases of Go 1 arise (Go 1.1, Go 1.2, etc.).


    The Go Blog: Toward Go 2

    This is the text of my [Russ Cox] talk today at Gophercon 2017, asking for the entire Go community's help as we discuss and plan Go 2.

    Finally, how will we ship and deliver Go 2?

    I think the best plan would be to ship the backwards-compatible parts of Go 2 incrementally, feature by feature, as part of the Go 1 release sequence.

    Once all the backwards-compatible work is done, say in Go 1.20, then we can make the backwards-incompatible changes in Go 2.0.

    This is all a bit speculative, and the specific release numbers I just mentioned are placeholders for ballpark estimates, but I want to make clear that we're not abandoning Go 1, and that in fact we will bring Go 1 along to the greatest extent possible.


    Compile and test a proof-of-concept program. For example,

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func dosomething() {}
    
    func donothing() {}
    
    func main() {
        var firstCommand, secondCommand string
        switch strings.ToLower(firstCommand) {
        default:
            fmt.Println("Default 1st Nest")
        case "c":
            fmt.Println("C CREATE")
            fallthrough
        case "u":
            fmt.Println("U UPDATE")
            fallthrough
        case "d":
            fmt.Println("D DELETE")
            dosomething()
    
            switch strings.ToLower(secondCommand) {
            default:
                fmt.Println("Default 2nd Nest")
            case "a":
                donothing()
            }
        }
    }
    

    Playground: https://play.golang.org/p/Q1ZdYqeNk67

    Output:

    Default 1st Nest
    

    Now, you have an authoritative answer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来