安裝我正在嘗試爲webhook編寫一些代碼,這將調用go install。我遇到的問題是,當我打電話去的任何命令與exec.Command運行go go使用golang 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())
fmt.Println(out.String())
}
func webhook(w http.ResponseWriter, r *http.Request) {
exec_cmd(w, "go", "install", "github.com/me/myrepo/mything")
}
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)
}
當網絡掛接端點打GOPATH沒有設置,它提供了:
exit status 1: can't load package: package github.com/me/myrepo/mything: cannot find package "github.com/me/myrepo/mything" in any of:
/usr/lib/go-1.6/src/github.com/me/myrepo/mything (from $GOROOT)
($GOPATH not set)
將如何我是否確定GOPATH是在這種情況下設置的?
如果我從命令行運行「go install github.com/me/myrepo/mything」,它工作正常。
在https://golang.org/pkg/os/exec/#Cmd中填寫'Env'。 – Volker
你也應該更新你的Go版本,如果只是因爲go1.8引入了默認的GOPATH。 – JimB