如何使用exec.Command和多个参数正确地进行”go build”操作?

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

How to properly "go build" using exec.Command with many arguments?

问题

我正在尝试使用exec.Command编译一个Go包。当参数为"go"和"build"时,我成功了,代码如下:

package main

import (
	"fmt"
	"log"
	"os/exec"
)

func main() {
	out, err := exec.Command("go", "build").Output()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(out)
}

然而,当我尝试使用更多参数执行"go build"时,似乎我做错了什么?以下是我的代码:

package main

import (
	"fmt"
	"log"
	"os/exec"
)

func main() {
	out, err := exec.Command("set", "GOOS=js&&", "set", "GOARCH=wasm&&", "go", "build", "-o", "C:/Users/Daniel/Desktop/go-workspace/src/sandbox/other/wasm/assets/json.wasm", "kard").Output()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(out)
}

输出是exec: "set": 可执行文件在 %PATH% 中未找到

在命令行中,我通常执行的命令是set GOOS=js&& set GOARCH=wasm&& go build -o C:\Users\Daniel\Desktop\go-workspace\src\sandbox\other\wasm\assets\json.wasm kard

我猜我对使用exec.Command有所误解?非常感谢您的支持。

英文:

I'm trying to compile a go package using exec.Command. I was successful with the arguments being "go" and "build" as such below:

package main

import (
	"fmt"
	"log"
	"os/exec"
)

func main() {
	out, err := exec.Command("go", "build").Output()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(out)
}

However, while trying to perform a "go build" with more arguments it seems that I'm doing something wrong? This is what my code looks like:

package main

import (
	"fmt"
	"log"
	"os/exec"
)

func main() {
	out, err := exec.Command("set", "GOOS=js&&", "set", "GOARCH=wasm&&", "go", "build", "-o", "C:/Users/Daniel/Desktop/go-workspace/src/sandbox/other/wasm/assets/json.wasm", "kard").Output()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(out)
}

The output is exec: "set": executable file not found in %PATH%

The normal command I would perform for this in the command line would be set GOOS=js&& set GOARCH=wasm&& go build -o C:\Users\Daniel\Desktop\go-workspace\src\sandbox\other\wasm\assets\json.wasm kard.

I assume there is something I'm misunderstanding with using exec.Command? I really appreciate any support.

答案1

得分: 4

应用程序使用shell语法来设置环境变量,但exec包不使用shell(除非您运行的命令是shell命令)。

使用command environment来为exec的命令指定环境变量。

go build通常不会写入stdout,但会将错误写入stderr。使用CombinedOutput而不是Output可以轻松捕获错误文本。

cmd := exec.Command("go", "build", "-o", "C:/Users/Daniel/Desktop/go-workspace/src/sandbox/other/wasm/assets/json.wasm", "kard")
cmd.Env = []string{"GOOS=js", "GOARCH=wasm"}
out, err := cmd.CombinedOutput()
if err != nil {
    fmt.Printf("%v: %s\n", err, out)
}
英文:

The application uses shell syntax to set the environment variables, but the exec package does not use a shell (unless the command that you are running is a shell).

Use the command environment to specify the environment variables for the exec'ed command.

The go build does not normally write to stdout, but it does write errors to stderr. Use CombinedOutput instead of Output to easily capture error text.

cmd := exec.Command("go", "build", "-o", "C:/Users/Daniel/Desktop/go-workspace/src/sandbox/other/wasm/assets/json.wasm", "kard")
cmd.Env = []string{"GOOS=js", "GOARCH=wasm"}
out, err := cmd.CombinedOutput()
if err != nil {
    fmt.Printf("%v: %s\n", err, out)
}

huangapple
  • 本文由 发表于 2022年10月3日 03:36:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/73928792.html
匿名

发表评论

匿名网友

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

确定