Skip to content

Instantly share code, notes, and snippets.

@kraxarn
Last active December 28, 2019 13:01
Show Gist options
  • Save kraxarn/963d909b917c463a6c782310eacdc09c to your computer and use it in GitHub Desktop.
Save kraxarn/963d909b917c463a6c782310eacdc09c to your computer and use it in GitHub Desktop.
Binary file to Go source file
/*
* Generate data
*
* Converts a file to a Go source file.
* Go source file gets generated as data.go with an appData constant containing the file data as base64.
*
* Licensed under WTFPL.
*/
package main
import (
"encoding/base64"
"fmt"
"io/ioutil"
"os"
)
func main() {
if len(os.Args) != 2 {
fmt.Println("usage: gendata <input>")
return
}
out, _ := ioutil.ReadFile(os.Args[1])
data := fmt.Sprintf("package main\n\nconst appData = \"%v\"", base64.StdEncoding.EncodeToString(out))
ioutil.WriteFile("data.go", []byte(data), 0644)
}
/*
* Go to base64
*
* Converts a file into a base64 string.
* Basically the same as base64, just made in Go to unsure compataiblity with Go applications.
*
* Licensed under WTFPL.
*/
package main
import (
"encoding/base64"
"fmt"
"io/ioutil"
"os"
)
func main() {
if len(os.Args) != 2 {
fmt.Println("usage: gotob64 <input>")
return
}
out, _ := ioutil.ReadFile(os.Args[1])
fmt.Printf(base64.StdEncoding.EncodeToString(out))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment