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.