Golang Windows CMD 文件复制:简单代码示例
package main
import ( "fmt" "io" "os" )
func main() { sourceFile := 'C:/Users/username/Documents/source.txt' destinationFile := 'C:/Users/username/Documents/destination.txt'
// 打开源文件
source, err := os.Open(sourceFile)
if err != nil {
fmt.Println('无法打开源文件:', err)
os.Exit(1)
}
defer source.Close()
// 创建目标文件
destination, err := os.Create(destinationFile)
if err != nil {
fmt.Println('无法创建目标文件:', err)
os.Exit(1)
}
defer destination.Close()
// 复制文件
_, err = io.Copy(destination, source)
if err != nil {
fmt.Println('复制文件失败:', err)
os.Exit(1)
}
fmt.Println('文件复制成功!')
}
原文地址: https://www.cveoy.top/t/topic/n9Mi 著作权归作者所有。请勿转载和采集!