How to print exit status in golang?
April 08, 2022Hi Friends š,
Welcome To aGuideHub! ā¤ļø
Today, Iām going to show you how to print exit status in golang, well in this tutorial we will use the log.Printf()
method to print exit status.
luckily, i have found some exit status code example, here we have to focus log.Printf("Exit Status: %d", status.ExitStatus())
line.
package main
import "os/exec"
import "log"
import "syscall"
func main() {
cmd := exec.Command("git", "blub")
if err := cmd.Start(); err != nil {
log.Fatalf("cmd.Start: %v", err)
}
if err := cmd.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
log.Printf("Exit Status: %d", status.ExitStatus())
}
} else {
log.Fatalf("cmd.Wait: %v", err)
}
}
}
When we run the above code in the golang compiler, we get an error plus an Exit Status log.
Check the output of the above code example.
All the best š