use runtime to get os info

This commit is contained in:
tinylcy
2017-03-10 16:11:08 +08:00
parent e874c5eb16
commit e8f7a0c3c0

View File

@@ -113,7 +113,7 @@ var (
a := 1 a := 1
``` ```
下面这个例子展示了如何在运行时获取所在的操作系统类型,通过 `os` 包中的函数 `os.Getenv()` 来获取环境变量中的值,并保存到 string 类型的局部变量 path 中。 下面这个例子展示了如何通过`runtime`在运行时获取所在的操作系统类型,以及如何通过 `os` 包中的函数 `os.Getenv()` 来获取环境变量中的值,并保存到 string 类型的局部变量 path 中。
示例 4.5 [goos.go](examples/chapter_4/goos.go) 示例 4.5 [goos.go](examples/chapter_4/goos.go)
@@ -122,11 +122,12 @@ package main
import ( import (
"fmt" "fmt"
"runtime"
"os" "os"
) )
func main() { func main() {
var goos string = os.Getenv("GOOS") var goos string = runtime.GOOS
fmt.Printf("The operating system is: %s\n", goos) fmt.Printf("The operating system is: %s\n", goos)
path := os.Getenv("PATH") path := os.Getenv("PATH")
fmt.Printf("Path is %s\n", path) fmt.Printf("Path is %s\n", path)