如何在Go语言程序中运行二进制文件?

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

How to run Binary Files inside GoLang Program?

问题

我想在Go语言程序中执行二进制文件。

这是我的代码:

package main

import (
	"fmt"
	"os/exec"
)

func main() {
	output, _ := exec.Command("/home/user/Golang/bin/hello").Output()
	fmt.Println(output)
}

但是我得到的输出是:[]

提前感谢。

英文:

I want to execute Binary Files inside GoLang Program.

Here is my code:

package main

import (
	"fmt"
	"os/exec"
)

func main() {
	output, _ := exec.Command("/home/user/Golang/bin/hello").Output()
	fmt.Println(output)
}

But I get the output as: []

Thanks in advance.

答案1

得分: 11

我可以获取输出。

package main

import (
	"fmt"
	"os/exec"
)

func main() {
	output, err := exec.Command("/Users/duguying/gopath/bin/test").Output()
	if err!=nil {
	    fmt.Println(err.Error())
    }
	fmt.Println(string(output))
}

首先检查你的二进制文件或二进制文件路径是否正确。尝试打印出错误消息。

英文:

I can get the output.

package main

import (
	"fmt"
	"os/exec"
)

func main() {
	output, err := exec.Command("/Users/duguying/gopath/bin/test").Output()
	if err!=nil {
	    fmt.Println(err.Error())
    }
	fmt.Println(string(output))
}

check you binary file first or binary filepath is correcting. try to print out your error message.

答案2

得分: 1

当我查看exec.Command()的源代码时,它并不返回错误,而是返回一个在exe包中的结构体Cmd
source

....

func Command(name string, arg ...string) *Cmd {
	cmd := &Cmd{
		Path: name,
		Args: append([]string{name}, arg...),
	}
	if filepath.Base(name) == name {
		if lp, err := LookPath(name); err != nil {
			cmd.lookPathErr = err
		} else {
			cmd.Path = lp
		}
	}
	return cmd
}

....

我成功地使用以下代码运行了二进制文件:

package main
    
import (
    "fmt"
    "os/exec"
)

func main() {
    command := exec.Command("你的二进制文件路径")
    
    // 设置变量以获取输出
    var out bytes.Buffer
     
    // 将输出设置为我们的变量
    command.Stdout = &out
    err = command.Run()
    if err != nil {
	    log.Println(err)
	}

    fmt.Println(out.String())
}

这段代码对我来说可以运行一个打印一些随机字符串的二进制文件。

英文:

When I'm looking at the source of the exec.Command() it doesnt return an error but only returns Cmd which is struct in the package exe :
source

....

func Command(name string, arg ...string) *Cmd {
	cmd := &Cmd{
		Path: name,
		Args: append([]string{name}, arg...),
	}
	if filepath.Base(name) == name {
		if lp, err := LookPath(name); err != nil {
			cmd.lookPathErr = err
		} else {
			cmd.Path = lp
		}
	}
	return cmd
}

....

I have succesfully got the binary file running using this code :

package main
    
import (

    "fmt"
    "os/exec"
)

func main() {
    command:= exec.Command("Your binary file path")
    
    // set var to get the output
    var out bytes.Buffer
     
     // set the output to our variable
     command.Stdout = &out
     err = command.Run()
     if err != nil {
	     log.Println(err)
	 }

    fmt.Println(out.String())
}

This one works for me for running a binary file that will print some random string.

huangapple
  • 本文由 发表于 2016年11月18日 13:53:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/40670228.html
匿名

发表评论

匿名网友

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

确定