2017-03-21 11:16
浏览 115

使用golang exec.Command运行go install

I'm trying to write some code for a webhook, that will call go install. The problem i'm having is that the GOPATH isn't set when i call any go commands with exec.Command

func exec_cmd(w http.ResponseWriter, cmd string, args ...string) {
    command := exec.Command(cmd, args...)
    var out bytes.Buffer
    var stderr bytes.Buffer
    command.Stdout = &out
    command.Stderr = &stderr
    err := command.Run()
    if err != nil {
        errstring := fmt.Sprintf(fmt.Sprint(err) + ": " + stderr.String())
        io.WriteString(w, errstring)
    io.WriteString(w, out.String())

func webhook(w http.ResponseWriter, r *http.Request) {
    exec_cmd(w, "go", "install", "")

func test(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "test")

func main() {
    mux := http.NewServeMux()
    mux.HandleFunc("/webhook", webhook)
    mux.HandleFunc("/", test)
    http.ListenAndServe(":8000", mux)

when the webhook endpoint is hit, it gives:

exit status 1: can't load package: package cannot find package "" in any of:
    /usr/lib/go-1.6/src/ (from $GOROOT)
    ($GOPATH not set)

How would i go about making sure the GOPATH is set in this context?

If i run "go install" from the command line, it works fine.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题