Go语言的switch case语法详解(附上与PHP的区别)


Go和其他语言switch case语法区别

Go的switch case

case匹配执行后,默认跳出判断,不需要 break 关键字。

Go正确写法:

switch e.Name {
        case "a", "b", "c":
            ...
        }

Go错误写法:

switch e.Name {
        case "a":
        case "b":
        case "c":
            ...
        }

PHP的switch case

case匹配执行后,继续执行下个case,跳出不再匹配下一个,要使用 break 关键字

switch (e.Name) {
        case "a":
        case "b":
        case "c":
            ...
        break;
}

golang 的 switch 遇到匹配的 case 后,执行完 case 内的代码会直接 break 出来,而 php 中需要手动 break,否则会一直往下匹配,直到找到中断位置结束。

Go语言常见switch case用法

1. 基本用法

func main() {
	var a = "hello"
	switch a {
	case "aaa":
		fmt.Println(11)
	case "hello":
		fmt.Println(1)
	case "world":
		fmt.Println(2)
	default:
		fmt.Println(0)
	}
}

2. 一分支case多个并列值

package main
 
import (
	"fmt"
)
 
func main() {
	//根据用户输入的月份,打印该月份所属的季节
	var month byte
	fmt.Print("请输入月份:")
	fmt.Scanln(&month)
	switch month {
		case 3, 4, 5:
			fmt.Println("春天")
		case 6, 7, 8:
			fmt.Println("夏天")
		case 9, 10, 11:
			fmt.Println("秋天")
		case 12, 1, 2:
			fmt.Println("冬天")
		default:
			fmt.Println("输入有误...")
	}
}

3. interface{}接口的类型判断

switch a := x.(type): 对于 interface{}接口变量,先赋值保存 变量值 备用,再 case 变量类型

package main
 
import (
	"fmt"
)
 
func main() {
	var x interface{}
	var y = 10
	x=y
	switch i := x.(type) {
	case nil:
		fmt.Printf("x的类型是:%T",i)
	case int:
		fmt.Printf("x是 int 类型")
	case float64:
		fmt.Printf("x是 float64 类型")
	case func(int) float64:
		fmt.Printf("x是func(int)类型")
	case bool,string:
		fmt.Printf("x是bool或者string类型")
	default:
		fmt.Printf("未知型")
	}
}

4. 跨越 case 的 fallthrough

Go语言中 case 执行后,不会接着执行下一个 case,但为了兼容一些移植代码,加入了 fallthrough 关键字。
加了fallthrough 后,会直接运行紧跟的后一个 case 或 default 语句,

package main
import "fmt"
func main() {
	var s = "hello"
	switch {
	case s == "world":
		fmt.Println("world")
	case s == "hello":
		fmt.Println("hello")
		fallthrough
	case s != "world":
		fmt.Println("world")
		fallthrough
	case s == "world":
		fmt.Println("world")
	}

}

5. 分支表达式

age := 19
switch {
case age < 18:
	fmt.Println("未成年")
case age >= 18:
	fmt.Println("已成年")
}

谈谈 PHP switch case 和其他语言的区别: https://xie.infoq.cn/article/d8af45c6a8a125c2615209fe5
golang switch case语句 简介: https://blog.csdn.net/whatday/article/details/113772678
golang:switch case: https://blog.csdn.net/zhizhengguan/article/details/104914687

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言switch case语法可以用于根据不同的条件执行不同的代码块。与其他语言相比,Go语言switch case语法有以下几个特点: 1. 基本用法: ```go switch 变量 { case 值1: // 执行代码块1 case2: // 执行代码块2 default: // 执行默认代码块 } ``` 这是最基本的switch case语法,根据变量的值选择执行相应的代码块。 2. 一分支case多个并列值: ```go switch 变量 { case 值1, 值2, 值3: // 执行代码块 } ``` 可以在一个case语句中指定多个并列的值,如果变量的值与任何一个值匹配,就会执行相应的代码块。 3. interface{}接口的类型判断: ```go switch 变量.(type) { case 类型1: // 执行代码块1 case 类型2: // 执行代码块2 default: // 执行默认代码块 } ``` 可以使用switch case语句对接口类型进行类型判断,根据不同的类型执行相应的代码块。 4. 跨越case的fallthrough: ```go switch 变量 { case 值1: // 执行代码块1 fallthrough case 值2: // 执行代码块2 } ``` 使用fallthrough关键字可以使程序执行完一个case后继续执行下一个case的代码块。 5. 分支表达式: ```go switch { case 表达式1: // 执行代码块1 case 表达式2: // 执行代码块2 default: // 执行默认代码块 } ``` 可以在switch语句中省略变量,直接使用表达式进行判断。 这些是Go语言switch case语法的一些常见用法。根据不同的需求,可以灵活运用这些特性来编写逻辑清晰的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值