在Go语言的switch语句中,每个case后面不需要使用break语句来跳出switch语句。如果希望多个case执行相同的操作,可以将这些case放在一起,使用逗号分隔。以下是一个示例:

package main

import "fmt"

func main() {
    num := 2

    switch num {
    case 1, 2, 3:
        fmt.Println('num is either 1, 2 or 3')
    case 4, 5, 6:
        fmt.Println('num is either 4, 5 or 6')
    default:
        fmt.Println('num is not in the range of 1 to 6')
    }
}

在上面的示例中,当num为1、2或3时,第一个case下面的代码将会执行;当num为4、5或6时,第二个case下面的代码将会执行;其他情况下,将会执行default下面的代码。


原文地址: https://www.cveoy.top/t/topic/pjcB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录