Skip to content

Instantly share code, notes, and snippets.

@Wneh
Last active December 21, 2015 06:49
Show Gist options
  • Save Wneh/6266913 to your computer and use it in GitHub Desktop.
Save Wneh/6266913 to your computer and use it in GitHub Desktop.
Convert the content of file in args[1] to base64 and saves it to the location in args[2] Example: $ go run base64convert ./foo.pdf ./fooInBase64.txt
package main
import (
"encoding/base64"
"fmt"
"os"
"io/ioutil"
)
// Take the file specified in args[1]
// and convert it to base64 and save
// save the result in the file specified
// in args[2]
func main() {
if len(os.Args) == 3 {
startFile := os.Args[1]
endFile := os.Args[2]
// read whole the file
b, err := ioutil.ReadFile(startFile)
if err != nil {
panic(err)
}
//Convert it
base64str := base64.StdEncoding.EncodeToString(b)
//Save it to file again
err = ioutil.WriteFile(endFile, []byte(base64str), 0644)
if err != nil {
panic(err)
}
fmt.Println("Done!")
} else {
fmt.Println("Wrong number of start arguments:")
fmt.Println("Arg 1: Filepath to read from")
fmt.Println("Arg 2: Filepath to save the converted version to")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment