「Go言語」switch文変数定義のサンプル

2022年1月22日

書式
switch 変数名:=処理式;変数名 {
case 値1:
処理コード1
case 値2:
処理コード2
default:
処理コード

}
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package main
import (
"fmt"
"runtime"
)
func main() {
switch os := runtime.GOOS; os {
case "linux":
fmt.Println("OSはLinuxです")
case "windows":
fmt.Println("OSはWindowsです")
default:
fmt.Printf("OS = %s", os)
}
}
package main import ( "fmt" "runtime" ) func main() { switch os := runtime.GOOS; os { case "linux": fmt.Println("OSはLinuxです") case "windows": fmt.Println("OSはWindowsです") default: fmt.Printf("OS = %s", os) } }
package main

import (
   "fmt"
   "runtime"
)

func main() {
   switch os := runtime.GOOS; os {
   case "linux":
      fmt.Println("OSはLinuxです")
   case "windows":
      fmt.Println("OSはWindowsです")
   default:
      fmt.Printf("OS = %s", os)
   }
}

実行結果
OSはLinuxです

Go言語

Posted by arkgame