【问题标题】:Shorten an || or if statement in Golang缩短 ||或者 Golang 中的 if 语句
【发布时间】:2020-11-07 15:17:34
【问题描述】:

学习 Golang 并想知道是否有更短的方法来编写这个

            if tiletype == 0 || tiletype == 2 {
                levelmap[passage1block] = "wall"
            } else {
                levelmap[passage1block] = "floor"
            }

本以为这样虽然行不通

                if tiletype ==0,2 {
            levelmap[passage1block] = "wall"
            } else {
                levelmap[passage1block] = "floor"
            }

【问题讨论】:

    标签: go shorthand


    【解决方案1】:

    你可以写一个 switch-case 语句:

    switch tiletype {
       case 0,2: levelmap[passage1block]="wall"
       default: levelmap[passage1block]="floor"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多