Last active
May 13, 2020 11:22
A Go application to copy the contents of a source file to a target file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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