Get exit code - Go

前端 未结 4 429
梦毁少年i
梦毁少年i 2020-12-04 13:52

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 rea

4条回答
  •  忘掉有多难
    2020-12-04 14:43

    Since golang version 1.12, the exit code is available natively and in a cross-platform manner. See ExitError and ExitCode().

    ExitCode returns the exit code of the exited process, or -1 if the process hasn't exited or was terminated by a signal.

    if err := cmd.Run() ; err != nil {
        if exitError, ok := err.(*exec.ExitError); ok {
            return exitError.ExitCode()
        }
    }
    

提交回复
热议问题