不像 Execute the 'cd' command for CMD in Go ,我只想使用 Go 真正运行 cd directory_location
并更改当前目录。
例如,
假设我在 ~/goproject 上,我在终端中运行 ./main
,我想在终端中位于 ~/goproject2。
我试过了
cmd := exec.Command("bash", "-c", "cd", "~/goproject2")
cmd.Run()
但这实际上并没有改变当前目录。
最佳答案
通常,如果您需要从特定目录运行命令,您可以将其指定为 Command 的 Dir 属性,例如:
cmd := exec.Command("myCommand", "arg1", "arg2")
cmd.Dir = "/path/to/work/dir"
cmd.Run()
关于go - 如何更改 Go 中的当前目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46028707/