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.

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

报告相同问题?

悬赏问题

  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 铜与钢双金属板叠加在一起每种材料300mm长,18mm宽,4mm厚一端固定并加热至80℃,当加热端温度保持不变时另一端的稳态温度。ansys
  • ¥15 django5安装失败
  • ¥15 Java与Hbase相关问题
  • ¥15 后缀 crn 游戏文件提取资源
  • ¥15 ANSYS分析简单钎焊问题
  • ¥20 bash代码推送不上去 git fetch origin master #失败了
  • ¥15 LOL外服加入了反作弊系统,现在游戏录像rofl文件离线都无法打开