package main

import (
	"fmt"
	"runtime"
)

func main()  {

    fmt.Println("Go runs on")
	switch os:=runtime.GOOS;os {
	case "darwin":
		fmt.Println("OS x.")
	case "linux":
		fmt.Println("Linux.")
	default:
		//其他系统
		fmt.Printf("%S.\n",os)
		
	}
}

Go 的 switch 语句类似于 C、C++、Java、JavaScript 和 PHP 中的,不过 Go 只运行选定的 case,而非之后所有的 case。 实际上,Go 自动提供了在这些语言中每个 case 后面所需的 break 语句。 除非以 fallthrough 语句结束,否则分支会自动终止。 Go 的另一点重要的不同在于 switch 的 case 无需为常量,且取值不必为整数。

相关文章:

  • 2022-01-12
  • 2021-05-19
  • 2021-08-17
  • 2021-04-18
  • 2022-12-23
  • 2022-03-10
  • 2021-09-24
猜你喜欢
  • 2021-12-25
  • 2022-12-23
  • 2021-06-28
  • 2021-05-29
  • 2021-09-19
  • 2021-12-09
  • 2021-06-18
相关资源
相似解决方案