从Go执行第三方二进制文件

huangapple go评论98阅读模式
英文:

Executing 3rd party binary from Go

问题

我在Go中进行了一点小的修改,以满足我的需求,即使用动态标志运行第三方可执行文件(这些标志取决于服务器设置和一些硬件规格,因此在每台机器和每次运行时都不同)。

我使用了一些很棒的库来帮助我找到Go可执行文件所在的路径。第三方二进制文件与Go二进制文件位于同一个文件夹中。

  path, err := osext.ExecutableFolder()
  if err != nil {
    log.Fatal(err)
  }
  path += "3rdparty.exe"

然后,我使用fmt的Sprintf方法将路径和标志放入名为Command的单个字符串中。

我尝试这样调用它:

out, err := exec.Command(Command).Output()
  if err != nil {
    fmt.Println("Command execution failed:", err)
  }

然而,err不是nil。我无法从vmware中复制错误(只是为了编译和测试而在那里设置了Windows),但它的内容大致是:
Command execution failed: "C:\\PATH\\TO\\3rdparty.exe --flags-omitted"文件不存在

然而,当我将C:\\PATH\\TO\\3rdparty.exe --flags-omitted复制到cmd中时,它可以正常运行。

有什么想法吗?

英文:

I made a little hack in Go for my needs to run 3rd party executable with dynamic flags (which depend on server settings and some hardware specs, so different on each machine and each time).

I'm using some neat library to help me out find which path is Go executable located in. The 3rd party binary is in same folder as Go one.

  path, err := osext.ExecutableFolder()
  if err != nil {
    log.Fatal(err)
  }
  path += "3rdparty.exe"

I than run the fmt's Sprintf method, to put path and flags into single string called Command.

I try to invoke it like so:

out, err := exec.Command(Command).Output()
  if err != nil {
    fmt.Println("Command execution failed:", err)
  }

However err isn't nil. I can't copy error out of vmware (setup windows there just to compile and test), but it goes like:
Command execution failed: "C:\\PATH\\TO\\3rdparty.exe --flags-omitted" file does not exist

However when I copy C:\\PATH\\TO\\3rdparty.exe --flags-omitted into cmd it runs perfectly.

Any ideas?

答案1

得分: 5

命令及其参数必须是单独的字符串,不要将它们连接成一个字符串。

仔细看一下,错误消息实际上对此非常清楚(请注意引号的位置):

命令执行失败:"C:\PATH\TO\3rdparty.exe --flags-omitted" 文件不存在

英文:

The command and its parametets must be separate strings, do not join them into a single string.

At a closer look, the error message actually is clear about it (note where the quotes are):

Command execution failed: "C:\\PATH\\TO\rdparty.exe --flags-omitted" file does not exist

huangapple
  • 本文由 发表于 2013年6月30日 17:29:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/17388969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定