【问题标题】:Executing 3rd party binary from Go从 Go 执行 3rd 方二进制文件
【发布时间】:2013-06-30 09:29:33
【问题描述】:

我在 Go 中做了一些小改动,因为我需要运行带有动态标志的 3rd 方可执行文件(这取决于服务器设置和一些硬件规格,因此在每台机器上和每次都不同)。

我正在使用一些简洁的库来帮助我找出 Go 可执行文件所在的路径。第 3 方二进制文件与 Go one 位于同一文件夹中。

  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 不是零。我无法从 vmware 中复制错误(设置窗口只是为了编译和测试),但它就像: Command execution failed: "C:\\PATH\\TO\\3rdparty.exe --flags-omitted" file does not exist

但是,当我将 C:\\PATH\\TO\\3rdparty.exe --flags-omitted 复制到 cmd 时,它运行完美。

有什么想法吗?

【问题讨论】:

    标签: go


    【解决方案1】:

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

    仔细一看,错误信息其实清楚的(注意引号在哪里):

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-02
      • 2013-08-04
      • 1970-01-01
      相关资源
      最近更新 更多