I’m using the package: os/exec http://golang.org/pkg/os/exec/ to execute a command in the operating system but I don’t seem to find the way to get the exit code. I can read the output though
ie.
package main
import(
"os/exec"
"bytes"
"fmt"
"log"
)
func main() {
cmd := exec.Command("somecommand", "parameter")
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run() ; err != nil {
//log.Fatal( cmd.ProcessState.Success() )
log.Fatal( err )
}
fmt.Printf("%q\n", out.String() )
}
It’s easy to determine if the exit code was 0 or something else. In the first case,
cmd.Wait()will return nil (unless there is another error while setting up the pipes).Unfortunately, there is no platform independent way to get the exit code in the error case. That’s also the reason why it isn’t part of the API. The following snippet will work with Linux, but I haven’t tested it on other platforms:
Just follow the api docs to find out more 🙂