Files
llgo/_demo/osproc/exec.go
2024-07-18 14:30:49 +08:00

32 lines
491 B
Go

package main
import (
"fmt"
"os"
"os/exec"
"runtime"
)
func main() {
fmt.Println("os.Environ:", os.Environ())
ls := "ls"
args := []string{ls, "-l"}
if runtime.GOOS == "windows" {
ls = "dir"
args = []string{ls}
}
lspath, _ := exec.LookPath(ls)
if lspath != "" {
ls = lspath
}
proc, err := os.StartProcess(ls, args, &os.ProcAttr{
Files: []*os.File{nil, os.Stdout, os.Stderr},
})
if err != nil {
fmt.Println("os.StartProcess error:", err)
return
}
proc.Wait()
}