package main import ( "fmt" "os" "syscall" ) func main() { cmd := exec.Command("ls", "-la") if err := cmd.Run(); err != nil { fmt.Printf("Command failed: %v\n", err) os.Exit(1) } if status, ok := cmd.ProcessState.Sys().(syscall.WaitStatus); ok { fmt.Printf("Exited with status %d\n", status.ExitStatus()) } }
package main import ( "fmt" "os" "os/exec" "syscall" ) func main() { cmd := exec.Command("sleep", "5") if err := cmd.Start(); err != nil { fmt.Printf("Command failed: %v\n", err) os.Exit(1) } if err := cmd.Wait(); err != nil { fmt.Printf("Command failed: %v\n", err) os.Exit(1) } if status, ok := cmd.ProcessState.Sys().(syscall.WaitStatus); ok { fmt.Printf("Exited with status %d\n", status.ExitStatus()) } }In this example, we are running the `sleep 5` command and waiting for it to complete. Then, we are using `cmd.ProcessState.Sys()` to get the exit status of the command, which is of type `syscall.WaitStatus`. Overall, the `WaitStatus` type is a useful tool for handling the exit status of a child process in Go programs. It is part of the `syscall` package library.