dsen53898 2017-03-20 09:31
浏览 95
已采纳

想要通过使用Go程序运行具有标志和参数的二进制文件

I want to run a binary file with flags. If i directly run the binary it will be like following inside a golang program.

./test --flag1 arg1 --flag2 arg2

I was trying to run by the use of os.exec.

code: reslt ,err:= exec.Command("./test","--flag1", "arg1", "--flag2", "arg2").Output

It is giving error:

Exit status 2

Can anyone help on this?

  • 写回答

2条回答 默认 最新

  • dongtuo1482 2017-03-20 11:00
    关注

    output, err := exec.Command("./test","flag1", "arg1", "flag2", "arg2").Output()

    Output returns both a slice of bytes and an error. As indicated by the error, you are only expecting a single return value, whereas Output returns two.

    EDIT: As for debugging your second problem, get the stderr from the command:

    cmd := exec.Command("./test","flag1", "arg1", "flag2", "arg2")
    var stderr bytes.Buffer
    cmd.Stderr = &stderr  
    err := cmd.Run()
    if err != nil {
        fmt.Println(stderr.String())
        return
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 halcon23.11安装
  • ¥15 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题