【发布时间】:2021-11-27 09:46:15
【问题描述】:
如果文件存在,如何创建具有给定名称的新文件
例如:如果 word_destination.txt 存在,则将内容复制到 word_destination(1).txt
任何帮助将不胜感激......
package main
import (
"fmt"
"io/ioutil"
"os"
)
func main() {
src := ./word_source.txt
desti := ./folder/word_destination.txt
//if file exists want to copy it to the word_destination(1).txt
if _, err := os.Stat(desti); err == nil {
// path/to/whatever exists
fmt.Println("File Exists")
} else {
fmt.Println("File does not Exists")
bytesRead, err := ioutil.ReadFile(src)
if err != nil {
log.Fatal(err)
}
【问题讨论】:
-
我投票结束这个问题,因为这个问题没有任何试图解决问题的迹象;它只有问题陈述和解决它的请求。
-
嗨!请阅读rules for posting——特别是关于所谓“家庭作业”的规则#3。也请考虑阅读this classic essay 解决问题的技巧。基本上,您的问题的主要问题是它不包含任何尝试实际解决您的问题的迹象;您刚刚陈述并要求解决方案。这种方法不会带你去任何地方。
标签: go file-copying