I wrote a short Go program using os.Exit(2)
and ran this from a Bash shell.
When I type echo $?
it shows a values of 1
regardless of the exit value passed to os.Exit
.
The Bash script below shows a $?
value of 2
as does the C program.
Why does the Go program always show a value of 1
? How can I exit with a code other than 0
or 1
, and should I use this method to indicate differing exit states?
package main
import "os"
func main() {
os.Exit(2)
}
#!/bin/bash
exit 2
#include <stdlib.h>
int main() {
exit(2);
}
The exit status 1
and 0
are not the exit status of your app but that of go run
.
If you run your app using go run
, then go run
will return 0
if your app returns 0
exit status, and 1
if your app returns a non-zero status (or go run
itself fails).
Build your app using go build
or go install
, then run your app directly. Then you will see a 2
exit status.
Quoting from Command go: Compile and run Go program:
The exit status of Run is not the exit status of the compiled binary.
Note: If you run your app on the Go playground, it also indicates the exit status of your app (with no output):
Program exited: status 2.
This "issue" was brought up before, see #13440. Russ Cox's words:
The real question is whether 'go run x.go' is supposed to be just an interpreter for Go programs, like 'python x.py' or whether it is a tool that runs a subprocess and reports the results itself, like make. To date, the answer has been the latter. So it's not obvious the behavior is wrong, unless 'go run' is supposed to be some kind of interactive go command, which we've said in the past it is not.
And Dmitri Shuralyov's words:
An exit code is a single-dimensional value. When doing go run, there are 2 processes that run and 2 exit codes one may want to know.
However, go run is only able to report a single exit code value, not two. It's not possible to losslessly combine two exit codes into one. If it reported the exit code of the program it ran verbatim, then information about the go run exit code would be shadowed and effectively lost.
IMO, if one cares about the exact exit code of the program that is executed, they need to build it and execute it themselves. go run is a convenience feature for when your needs are not as demanding and you're okay with less information, because it's unable to communicate more information than it already does.
Execute the program. For example,
exit.go
:
package main
import "os"
func main() {
os.Exit(2)
}
Output:
$ go build exit.go
$ ./exit
$ echo $?
2
$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With