package main import ( "os/exec" "fmt" ) func main() { cmd := exec.Command("ls") err := cmd.Run() if err != nil { fmt.Println(err) } }
package main import ( "os" "os/exec" "fmt" ) func main() { attr := &os.ProcAttr{ Dir: "/tmp", Env: []string{"PATH=/usr/bin"}, Files: []*os.File{ os.Stdin, os.Stdout, os.Stderr, }, } cmd := exec.Command("/bin/ls") cmd.Dir = "/tmp" cmd.Env = []string{"PATH=/usr/bin"} cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr process, err := os.StartProcess(cmd.Path, cmd.Args, attr) if err != nil { fmt.Println("Error starting process:", err) } else { fmt.Println("Process started:", process.Pid) } }This code runs the "ls" command with the attributes specified in the ProcAttr structure. It sets the current working directory to "/tmp", the environment variable "PATH" to "/usr/bin", and the file descriptors for standard input, output, and error to the current process's standard I/O. In conclusion, the ProcAttr structure is a useful package library that allows you to control various attributes of a process when starting it.