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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵