Copy a file in Go
How to copy a file in Go. The ioutil package does not offer a shorthand way of copying a file. Instead the os package should be used.
No shorthand method ¶
The ioutil
package offers some of the common operations needed when
working with files but nothing for copying files. Go tries to keep things
lightweight so as operations become more complex the os
package should be
used. The os
package operates at a slightly lower level and as such expects
that files are explicitly closed after opening them. Reading the source code of
the os
package shows that many of the functions in the ioutil
package are
wrappers around the os
package and remove the requirements to explicitly close
files.
Copying a file ¶
To copy a file is therefore a case of glueing together a few functions from the
os
package. The process is
- Open the file that should be copied
- Read the contents
- Create and open the file that the contents should be copied into
- Write to the new file
- Close both files
Code example ¶
The following shows an example of copying a file in Go.
package main
import (
"io"
"log"
"os"
)
func main() {
from, err := os.Open("./sourcefile.txt")
if err != nil {
log.Fatal(err)
}
defer from.Close()
to, err := os.OpenFile("./sourcefile.copy.txt", os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
log.Fatal(err)
}
defer to.Close()
_, err = io.Copy(to, from)
if err != nil {
log.Fatal(err)
}
}
Code example explaination ¶
The code example can be explained as follows.
- The
Open
function from theos
module is used to read a file from disk. - A defer statement is used to close the file once the script has finished all other execution.
- The
OpenFile
function is used to open a file. The first argument is the name of the file to be opened or created if it does not exist. The second argument represents the flags to be used on the file. In this case it is read and write and should be created if it does not exist. Finally the permissions on the file are set. - Another defer statement is used to close this file after other execution has finished.
- The
Copy
function is then used from theio
package. This copies from the source file and writes it to the destination.
Further reading ¶
Tags
Can you help make this article better? You can edit it here and send me a pull request.
See Also
-
Regular expressions in Go
Series of how to examples on using the regexp standard library in Go. Includes string, string index, string submatch, string submatch index and replace examples. -
Getting started with Go
Time to learn another language because I've got a restless brain. Next up Go. -
Linux and Unix watch command tutorial with examples
Tutorial on using watch, a UNIX and Linux command for executing a program periodically and showing a fullscreen output. Examples of watching a file download, a network interface come up, and showing the five most CPU intensive processes.