Last active
March 2, 2018 11:18
-
-
Save ruanbekker/a0a43fe7af46642d881474c5ef7f96cc to your computer and use it in GitHub Desktop.
Golang: Read File and Write to Disk with Arguments
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 ( | |
"io/ioutil" | |
"os" | |
"fmt" | |
) | |
var ( | |
input_filename string | |
output_filename string | |
) | |
func main() { | |
if len(os.Args) < 5 { | |
fmt.Printf("Usage: (-i/--input) 'input_filename' (-o/--output) 'output_filename' \n") | |
os.Exit(0) | |
} | |
for i, arg := range os.Args { | |
if arg == "-i" || arg == "--input" { | |
input_filename = os.Args[i+1] | |
} | |
if arg == "-o" || arg == "--output" { | |
output_filename = os.Args[i+1] | |
} | |
} | |
input_file_content, error := ioutil.ReadFile(input_filename) | |
if error != nil { | |
panic(error) | |
} | |
fmt.Println("File used for reading:", input_filename) | |
ioutil.WriteFile(output_filename, input_file_content, 0644) | |
fmt.Println("File used for writing:", output_filename) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment