In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how golang implements file operation. Xiaobian thinks it is quite practical, so share it with you for reference. I hope you can gain something after reading this article.
1. Copy of documents:
package mainimport ( "fmt" "io" "os" "strings")func main() { list := os.Args if len(list) != 3 { fmt.Println("Wrong input for whom, format: xxx.go source filename destination filename") return } fmt.Println("Source file path:", list[1], " ", "Destination file path:", list[2]) fmt.Println("Please confirm press Yy to continue: ") str := "" fmt.Scanln(&str) if strings.ToLower(str) == "y" { w, err := CopyFile(list[1], list[2]) if err != nil { fmt.Println(err) return } fmt.Println("Copy file complete, file size: ", w) } else { fmt.Println("User terminated action! ") }}func CopyFile(srcFileName, dstFileName string) (written int64, err error) { srcFile, err := os.Open(srcFileName) if err != nil { return } defer srcFile.Close() dstFile, err := os.OpenFile(dstFileName, os.O_WRONLY|os.O_CREATE, 0644) if err != nil { return } defer dstFile.Close() return io.Copy(dstFile, srcFile)}
Implementation results:
d:\goprojects\src\day1\Exercise>go run main.go d:\Understanding Parallel Programming V1.0.pdf d:\test.pdf Source file path: d:\Understanding Parallel Programming V1.0.pdf Target file path: d:\test. pdfPlease confirm press Yy to continue: y Copy file completed, file size: 4146835d:\goprojects\src\day1\Exercise>go run main.go d:\Deep understanding parallel programming V1.0.pdf d:\test. pdfSource file path: d:\Deep understanding parallel programming V1.0.pdf Destination file path: d:\test. pdfPlease confirm press Yy to continue: nUser terminated operation! d:\goprojects\src\day1\Exercise> About "golang how to achieve file operation" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.