dongtao9095 2019-04-17 16:02
浏览 103
已采纳

去os.Exit(2)显示bash $? 值1

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);
}

  • 写回答

2条回答 默认 最新

  • doushouxie7064 2019-04-17 16:08
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。