package main
import (
"io"
"log"
"os"
"path/filepath"
)
func main() {
sourceFile := "path/to/source/file.ext"
destinationDir := "path/to/destination/dir"
sourceFileName := filepath.Base(sourceFile)
backupFilePath := filepath.Join(destinationDir, sourceFileName)
source, err := os.Open(sourceFile)
if err != nil {
log.Fatalf("无法打开源文件:%v", err)
}
defer source.Close()
backup, err := os.Create(backupFilePath)
if err != nil {
log.Fatalf("无法创建备份文件:%v", err)
}
defer backup.Close()
_, err = io.Copy(backup, source)
if err != nil {
log.Fatalf("无法复制文件内容:%v", err)
}
log.Printf("文件备份完成。备份文件路径:%s", backupFilePath)
}