os.Getenv

显示PATH环境变量的值

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. )
  6. func main() {
  7. fmt.Println(os.Getenv("PATH"))
  8. }

os.exit

退出

  1. if err != nil {
  2. fmt.Printf("Program stopping with error %v", err)
  3. os.Exit(1)
  4. }

os.Args

参数列表

  1. import (
  2. "fmt"
  3. "os"
  4. "strings"
  5. )
  6. func main() {
  7. var who string
  8. if len(os.Args) > 1 {
  9. who = strings.Join(os.Args[1:], " ")
  10. }
  11. fmt.Println("Good Morning", who)
  12. }
  13. >go run main.go 222 333
  14. Good Morning 222 333

os.StartProcess

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. )
  6. func main() {
  7. env := os.Environ()
  8. procAttr := &os.ProcAttr{
  9. Env: env,
  10. Files: []*os.File{
  11. os.Stdin,
  12. os.Stdout,
  13. os.Stderr,
  14. },
  15. }
  16. _, err := os.StartProcess("/bin/ls", []string{"ls", "-l"}, procAttr)
  17. if err != nil {
  18. fmt.Printf("Error %v starting process!", err)
  19. os.Exit(1)
  20. }
  21. //fmt.Printf("The process id is %v", pid)
  22. }