Search code examples
goshorthand

Shorten an || or if statement in Golang


Learning Golang and was wondering if there is a shorter way to write this

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

Was thinking this would be the way though it does not work

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

Solution

  • You can write a switch-case statement:

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