cmd := exec.Command("ls", "-a") err := cmd.Run() if err != nil { log.Fatal(err) } fmt.Println("Command complete")
cmd := exec.Command("echo", "hello world!") out, err := cmd.Output() if err != nil { log.Fatal(err) } fmt.Printf("Command output: %s", out)
cmd := exec.Command("false") err := cmd.Run() if err != nil { if exitError, ok := err.(*exec.ExitError); ok { fmt.Printf("Command failed with error: %s\n", exitError.Stderr) } else { log.Fatal(err) } }In this example, the `false` command is executed, which always returns an error. The `Run` method waits for the command to complete execution and returns an error if the command fails to execute. If the command fails with a non-zero exit code, an `*exec.ExitError` is returned, which contains the stderr output of the command.