「Go言語」switch文変数定義のサンプル
書式
switch 変数名:=処理式;変数名 {
case 値1:
処理コード1
case 値2:
処理コード2
default:
処理コード
}
使用例
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です