我可以使用这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()

Go相关问答推荐

Term~T中的类型不能是类型参数,但可以是引用类型参数的切片

验证访问令牌(密钥罩)

使用ciph.AEAD.Seal()查看内存使用情况

如何使用Gorilla WebSockets实现Http.Hijacker&;alexedwards/scs/v2

如何找到一个空闲的TPM句柄来保存新的密钥对对象?

Go 中如何判断 struct 体是否包含另一个 struct 体?

Go - 永远停止带有上下文的循环

Golang Docker Selenium Chrome

如何为ANTLR4目标Go调试监听器

通过多阶段构建复制到 Docker 容器中时找不到文件

Go 中的 YAML 自定义标签

golang gin 获取 cookie json

更改多对多连接表的名称

Gorm 在自定义字符串类型上返回 scanner 错误

将 firestoreinteger_value转换为整数

Go:如何在将 float64 转换为 float32 时判断精度损失

无法使用 gocsv 读取引用字段

在 go 中将运行命令的标准输出发送到其标准输入

HCL 解码:具有多个标签的块

有没有办法在一个 goroutine 返回后延迟后取消上下文?