我可以使用这out, err := exec.Command("git", "log").Output()
来获得命令的输出,该命令将在与可执行文件位置相同的路径中运行.
如何指定要在哪个文件夹中运行该命令?
我可以使用这out, err := exec.Command("git", "log").Output()
来获得命令的输出,该命令将在与可执行文件位置相同的路径中运行.
如何指定要在哪个文件夹中运行该命令?
exec.Command()
将返回*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()