转到 os.Exit(2) 显示 bash $?值为 1
Go os.Exit(2) show a bash $? value of 1
我使用 os.Exit(2)
和 运行 从 Bash shell.
中编写了一个简短的 Go 程序
当我键入 echo $?
时,无论传递给 os.Exit
.
的退出值如何,它都会显示 1
的值
下面的 Bash 脚本显示了 2
的 $?
值,C 程序也是如此。
为什么Go程序总是显示1
的值?我如何使用 0
或 1
以外的代码退出,我是否应该使用此方法来指示不同的退出状态?
package main
import "os"
func main() {
os.Exit(2)
}
#!/bin/bash
exit 2
#include <stdlib.h>
int main() {
exit(2);
}
退出状态 1
和 0
不是您应用的退出状态,而是 go run
。
如果您 运行 您的应用使用 go run
,那么 go run
将 return 0
如果您的应用 returns 0
退出状态,并且 1
如果您的应用 return 是非零状态(或者 go run
本身失败)。
使用 go build
或 go install
构建您的应用,然后直接 运行 您的应用。然后你会看到一个 2
退出状态。
引用自Command go: Compile and run Go program:
The exit status of Run is not the exit status of the compiled binary.
注意:如果您 运行 您的应用程序在 Go playground 上,它还会指示您的应用程序的退出状态(无输出):
Program exited: status 2.
这个“问题”之前有人提出过,见#13440。 Russ Cox的话:
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.
Dmitri Shuralyov 的话:
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.
执行程序。例如,
exit.go
:
package main
import "os"
func main() {
os.Exit(2)
}
输出:
$ go build exit.go
$ ./exit
$ echo $?
2
$
我使用 os.Exit(2)
和 运行 从 Bash shell.
当我键入 echo $?
时,无论传递给 os.Exit
.
1
的值
下面的 Bash 脚本显示了 2
的 $?
值,C 程序也是如此。
为什么Go程序总是显示1
的值?我如何使用 0
或 1
以外的代码退出,我是否应该使用此方法来指示不同的退出状态?
package main
import "os"
func main() {
os.Exit(2)
}
#!/bin/bash
exit 2
#include <stdlib.h>
int main() {
exit(2);
}
退出状态 1
和 0
不是您应用的退出状态,而是 go run
。
如果您 运行 您的应用使用 go run
,那么 go run
将 return 0
如果您的应用 returns 0
退出状态,并且 1
如果您的应用 return 是非零状态(或者 go run
本身失败)。
使用 go build
或 go install
构建您的应用,然后直接 运行 您的应用。然后你会看到一个 2
退出状态。
引用自Command go: Compile and run Go program:
The exit status of Run is not the exit status of the compiled binary.
注意:如果您 运行 您的应用程序在 Go playground 上,它还会指示您的应用程序的退出状态(无输出):
Program exited: status 2.
这个“问题”之前有人提出过,见#13440。 Russ Cox的话:
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.
Dmitri Shuralyov 的话:
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.
执行程序。例如,
exit.go
:
package main
import "os"
func main() {
os.Exit(2)
}
输出:
$ go build exit.go
$ ./exit
$ echo $?
2
$