January 2018
Intermediate to advanced
340 pages
8h 6m
English
The following example uses the io.Copy() function to copy the contents from one reader to another writer:
package main
import (
"io"
"log"
"os"
)
func main() {
// Open original file
originalFile, err := os.Open("test.txt")
if err != nil {
log.Fatal(err)
}
defer originalFile.Close()
// Create new file
newFile, err := os.Create("test_copy.txt")
if err != nil {
log.Fatal(err)
}
defer newFile.Close()
// Copy the bytes to destination from source
bytesWritten, err := io.Copy(newFile, originalFile)
if err != nil {
log.Fatal(err)
}
log.Printf("Copied %d bytes.", bytesWritten)
// Commit the file contents
// Flushes memory to disk
err = newFile.Sync()
if err != nil {
log.Fatal(err)
}
}
Read now
Unlock full access