我可以使用它out, err := exec.Command("git", "log").Output()
来获取命令的输出,该命令将在与可执行位置相同的路径中运行。
如何指定要在哪个文件夹中运行命令?
exec.Command()
返回一个type值*exec.Cmd
。Cmd
是一个struct并具有一个Dir
字段:
// Dir specifies the working directory of the command.
// If Dir is the empty string, Run runs the command in the
// calling process's current directory.
Dir string
因此,只需在调用之前进行设置Cmd.Output()
:
cmd:= exec.Command("git", "log")
cmd.Dir = "your/intended/working/directory"
out, err := cmd.Output()
还要注意,这是特定于git
命令的。git
允许您使用-C
标志传递路径,因此您也可以像这样进行操作:
out, err := exec.Command("git", "-C", "your/intended/working/directory", "log").
Output()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句