Last active
May 11, 2020 14:05
-
-
Save dtoebe/d36c50207faee76b0305927c0422ad0e to your computer and use it in GitHub Desktop.
EMBED OTHER BINARIES IN GOLANG BINARY - generators.go - 1
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
// ./generators/generator.go | |
package main | |
//Much of this file is from http://stackoverflow.com/questions/17796043/golang-embedding-text-file-into-compiled-executable | |
import ( | |
"fmt" | |
"io/ioutil" | |
"os" | |
"strings" | |
) | |
func main() { | |
//Empty string slice to place the byte lierals in | |
var dataSlice []string | |
//This will be the generated go file | |
//Note: the path is ./ because we are using the go generate command | |
// inside the main.go file | |
outfile, err := os.Create("./generateddata.go") | |
if err != nil { | |
panic(err.Error()) | |
} | |
defer outfile.Close() | |
//This is the file we will turn to []bytes | |
//Make sure you use the name you used to build the sample data | |
infile, err := ioutil.ReadFile("./sample-data/generated-bin") | |
if err != nil { | |
panic(err.Error()) | |
} | |
//Write the initial data to the generated go file | |
//package main | |
//var data = []byte{ EOF... so far | |
outfile.Write([]byte("package main\n\nvar (\n\tdata = []byte{")) | |
//Here we loop over each byte in the []byte from the sample | |
//data file. | |
//Take the byte literal and format it as a string | |
//Then append it to the empty []string dataSlice we created at | |
//the top of the func | |
//Depending on the size of infile this could take a bit | |
for _, b := range infile { | |
bString := fmt.Sprintf("%v", b) | |
dataSlice = append(dataSlice, bString) | |
} | |
//We join the []string together separating it with commas | |
//Remember we are writing a go src file so everything has to be a string | |
dataString := strings.Join(dataSlice, ", ") | |
//Write all the data to the generated file | |
outfile.WriteString(dataString) | |
//Finish off the data | |
outfile.Write([]byte("}\n)")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment