Created
August 23, 2019 11:48
-
-
Save rabingaire/5a74f287cd3fca697ecd4537044ae732 to your computer and use it in GitHub Desktop.
text2go: Converts .txt file to .go 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 ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
) | |
func main() { | |
args := os.Args[1:] | |
if len(args) < 3 { | |
log.Fatal("pass all the required arguments") | |
} | |
inputFile := args[0] | |
outputFile := args[1] | |
packageName := args[2] | |
ifile, err := os.Open(inputFile) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer ifile.Close() | |
data, err := ioutil.ReadAll(ifile) | |
if err != nil { | |
log.Fatal(err) | |
} | |
character := '\x60' | |
newData := fmt.Sprintf(` | |
package %s | |
var Value string = %c%s%c`, packageName, character, data, character) | |
// Create a file | |
ofile, err := os.Create(outputFile) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer ofile.Close() | |
_, err = ofile.WriteString(newData) | |
if err != nil { | |
log.Fatalf("failed writing to file: %s", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First Argument: Input file
Second Argument: Output file
Third Argument: Package name