mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:33:04 +08:00
use runtime to get os info
This commit is contained in:
@@ -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)
|
||||||
|
Reference in New Issue
Block a user