Golang:用它的参数执行命令

Ale*_*. b 7 git command-line command go

我正在尝试使用go执行命令.

executableCommand := strings.Split("git commit -m 'hello world'", " ")
executeCommand(executableCommand[0], executableCommand[1:]...)
cmd := exec.Command(command, args...)
Run Code Online (Sandbox Code Playgroud)

但这就是我得到的

error: pathspec 'world"' did not match any file(s) known to git.
exit status 1
Run Code Online (Sandbox Code Playgroud)

这是因为-m获得'hello的,而不是'hello world'因为在命令行中使用分裂" ".

有什么想让它发挥作用吗?

Grz*_*Żur 13

如果没有解释引号等的shell的帮助,你想要的实际上很难实现.所以你可以使用shell来运行你的命令.

exec.Command("sh", "-c", "echo '1 2 3'")
Run Code Online (Sandbox Code Playgroud)