如果我在目录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()
答案 0 :(得分:2)
"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())