如果我在目录 A 中运行 GO 代码,我需要将文件从目录 B 复制到目录 C ,该怎么做?我尝试添加 cmd.Dir = "B"但它可以复制 "B"目录中的文件,但是当我尝试目录 "C"的完整路径时它会抛出错误 "退出状态 1"
基本代码示例
当前在目录 A 中,位置为“/var/A” cmd := exec.Command("cp","/var/C/c.txt","/var/B/") 错误 := cmd.Run()
请您参考如下方法:
"os/exec"是用于运行外部程序的 Go 包,其中包括 Linux 实用程序。
// The command name is the first arg, subsequent args are the
// command arguments.
cmd := exec.Command("tr", "a-z", "A-Z")
// Provide an io.Reader to use as standard input (optional)
cmd.Stdin = strings.NewReader("some input")
// And a writer for standard output (also optional)
var out bytes.Buffer
cmd.Stdout = &out
// Run the command and wait for it to finish (the are other
// methods that allow you to launch without waiting.
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
fmt.Printf("in all caps: %q\n", out.String())