检测当前操作系统类型
runtime.GOOS常量可用于在运行时检测操作系统,因为该常量仅在运行时设置。
runtime.GOOS常量可用于在运行时检测操作系统,因为该常量仅在运行时设置。
package main
import (
"fmt"
"runtime"
)
func main() {
os := runtime.GOOS
switch os {
case "windows":
fmt.Println("Windows")
case "darwin":
fmt.Println("MacOS")
case "linux":
fmt.Println("Linux")
default:
fmt.Printf("%s.\n", os)
}
}
MacOS