Skip to content

Instantly share code, notes, and snippets.

@roelofjan-elsinga
Last active May 13, 2020 11:22
Show Gist options
  • Save roelofjan-elsinga/69826bce71435742d9a343d1867486f0 to your computer and use it in GitHub Desktop.
Save roelofjan-elsinga/69826bce71435742d9a343d1867486f0 to your computer and use it in GitHub Desktop.
A Go application to copy the contents of a source file to a target file
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
)
func main() {
var sourceFile = flag.String(
"source",
"source.json",
"The file to use as a source to copy from",
)
var targetFile = flag.String(
"target",
"target.json",
"The file to use as a target to write to",
)
flag.Parse()
fileContents, readErr := ioutil.ReadFile(*sourceFile)
if readErr != nil {
fmt.Println("Found an error while reading the source file: " + readErr.Error())
os.Exit(1)
}
writeErr := ioutil.WriteFile(*targetFile, fileContents, 0644)
if writeErr != nil {
fmt.Println("Found an error while writing to the target file: " + writeErr.Error())
os.Exit(1)
}
fmt.Println("Copied the contents of " + *sourceFile + " to " + *targetFile)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment