Copy files using the Go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

How do I use the Go language to copy a file into another file? The Go Language standard package IO provides a simple function copy to implement this function, here is an example.

package mainimport (    "fmt"    "io"    "os")func main() {    CopyFile(os.Args[1], os.Args[2]) // os.Args[1]为目标文件,os.Args[2]为源文件    fmt.Println("复制完成",)}func CopyFile(dstName, srcName string) (written int64, err error) {    src, err := os.Open(srcName)    if err != nil {        return    }    defer src.Close()    dst, err := os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE, 0644)    if err != nil {        return    }    defer dst.Close()    return io.Copy(dst, src)}

e:\go_source\Go_program_code\temptest>temptest.exe abc.log ex14061310.logCopy 2917891 bytes! E:\gosource\goprogram_code\temptest>ls-al Total 3947 drwxr-xr-x 2 sharp administ 0 OCT 24 10:23. Drwxr-xr-x Sharp administ 8192 Oct 20 17:20.. -rw-r--r--1 administ 2917891 Oct 11:05 abc.log-rw-r--r--1 Sharp Admini St 2917891 June 10:00am ex14061310.log-rwxr-xr-x 1 sharp administ 2236928 Oct 11:05 temptest.exe-r w-r--r--1 Sharp administ 520 OCT 11:05 Temptest.go

Note the use of defer here, after the source file and the destination file are opened, followed by a defer delay closing the file. If defer DST is not used after the destination file. Close (), once the creation of the target file fails to debug, it will return the error directly, causing the source file to remain open so that the resource is not released. Therefore, in the go language, remember that the open file has been incorrectly judged to be followed by a defer close delay call.

Io. Copy's function prototype is as follows func Copy (DST Writer, SRC Reader) (written int64, err error)
The copy function copies from source to destination until it reads EOF to the source or if there are other errors, it returns the number of bytes copied and the first error that occurred during the copy process. If replication succeeds, the COP returns ERR ==nil, because copy is defined as copy from source until EOF is encountered, it does not treat EOF as an error. There is also an IO in the IO packet. Copyn function, the prototype is as follows func Copyn (DST Writer, Src Reader, n Int64) (written int64, err error)Copyn will copy n bytes from the source file (or encounter an error interrupt) to the target file and return to the number of sections actually copied
package mainimport (    "fmt"    "io"    "os")func main() {    counter,  := CopyFile(os.Args[1], os.Args[2], os.Args[3]) // os.Args[1]为目标文件,os.Args[2]为源文件     fmt.Println("复制", counter, "字节!")}func CopyFile(dstName, srcName string, n int64) (written int64, err error) {    src, err := os.Open(srcName)    if err != nil {        return    }    defer src.Close()    dst, err := os.OpenFile(dstName, os.OWRONLY|os.OCREATE, 0644)     if err != nil {        return    }    defer dst.Close()    return io.CopyN(dst, src, n) //}
E:\go_source\go_program_code\temptest>temptest.exe test.go temptest.go 1000 copy 526 Bytes!
E:\go_source\go_program_code\temptest>ls-al Total 3948 drwxr-xr-x 2 sharp administ 0 OCT 24 11:10. Drwxr-xr-x Sharp administ 8192 Oct 20 17:20.. -rw-r--r--1 Sharp administ 2917891 Oct 11:05 abc.log-rw-r--r--1 sharp administ 2917891 June 10:00am Ex14061310.log- Rwxr-xr-x 1 administ 2236928 Oct 11:05 temptest.exe-rw-r--r--1 Sharp administ 526 OCT 11:10 temptest.go-rw-r--r--1 Sharp administ 526 OCT 11:10 Test.go

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.