os.Getenv
显示PATH环境变量的值
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println(os.Getenv("PATH"))
}
os.exit
退出
if err != nil {
fmt.Printf("Program stopping with error %v", err)
os.Exit(1)
}
os.Args
参数列表
import (
"fmt"
"os"
"strings"
)
func main() {
var who string
if len(os.Args) > 1 {
who = strings.Join(os.Args[1:], " ")
}
fmt.Println("Good Morning", who)
}
>go run main.go 222 333
Good Morning 222 333
os.StartProcess
package main
import (
"fmt"
"os"
)
func main() {
env := os.Environ()
procAttr := &os.ProcAttr{
Env: env,
Files: []*os.File{
os.Stdin,
os.Stdout,
os.Stderr,
},
}
_, err := os.StartProcess("/bin/ls", []string{"ls", "-l"}, procAttr)
if err != nil {
fmt.Printf("Error %v starting process!", err)
os.Exit(1)
}
//fmt.Printf("The process id is %v", pid)
}