Copy files using the Go language

Source: Internet
Author: User

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 Main
Import(
  "FMT"
  "io"
  "OS"
)
FuncMain (){
 CopyFile (OS. args[1],os. args[2])//os. ARGS[1] for the target file, OS. ARGS[2] As the source file
 FMT. Println ("Copy Complete",)
}
FuncCopyFile (dstname,srcnamestring)(written   Int64,errerror){
 src,err: =os. Open (SrcName)
  iferr!=nil{
  return
    }
  defersrc. Close ()
 DST,err: =os. OpenFile (DstName,os. O_wronly|os. O_create,0644)
  iferr!=nil{
  return
    }
  deferDST. Close ()
  returnio. Copy (DST,src)
}
E:\go_source\go_program_code\temptest>temptest.exe abc.log ex14061310.log Copy 2917891  Bytes!
E:\go_source\go_program_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 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 520 Oct 11:05 temptest.go/////Note here the use of defer, after the source files and destination files 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 Main
Import(
  "FMT"
  "io"
  "OS"
)
FuncMain (){
    Counter, _ := CopyFile(Os.Args[1], Os.Args[2], Os.Args[3]) // OS. ARGS[1] for the target file, OS. ARGS[2] As the source file
 fmt. Println("copy",counter,"Byte!" ) )
}
 func    copyfile ( dstname   Srcname  string    n  int64  )     ( written   int64  ,    err  error  )    { 
 src,err: =os. Open(srcname)
  if Err! =Nil {
  return
    }
  defersrc. Close()
       dst,    err  :=   os Openfile ( dstname   os O_wronly|  Os O_create    0644  )  
  if Err! =Nil {
  return
    }
  deferDST. Close()
  returnio. 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 administ 2917891 Oct 11:05 abc.log-rw-r--r--1 sharp administ 2917891 June 10:00am-Ex 14061310.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

Copy files using the Go language

Related Article

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.