Embed Text Files
If we are talking about text files, they can be easily embedded in the source code itself. Just use backticks to declare a string literal as follows:
const html = ' <html> <body>Example embedded HTML content.</body> </html> '
Optimization tip:
Since in most cases you only need to write the resource to io.Writer , you can also save the result of the conversion []byte :
var html = []byte(' <html><body>Example...</body></html> ')
The only thing you should be careful with is that raw string literals cannot contain the backtick character ('). Raw string literals cannot contain sequences (as opposed to interpreted string literals), so if the text you want to embed contains backquotes, you must break the raw string literal and concatenate backquotes as interpreted string literals, as in this example:
var html = '<p>This is a back quote followed by a dot: ' + "'" + '.</p>'
This does not affect performance, since these associations will be performed by the compiler.
Embedding Binary Files
Byte fragment storage
For binary files (for example, images) the most compact (relative to the resulting own binary file) and the most efficient will be the contents of the file in the form of []byte in the source code. This can be generated by third-party tasks / libraries such as go-bindata .
If you do not want to use a third-party library for this, here is a simple snippet of code that reads a binary file and displays the Go source code, which declares a variable []byte type []byte that will be initialized with the exact contents of the file:
imgdata, err := ioutil.ReadFile("someimage.png") if err != nil { panic(err) } fmt.Print("var imgdata = []byte{") for i, v := range imgdata { if i > 0 { fmt.Print(", ") } fmt.Print(v) } fmt.Println("}")
Example output if the file contains bytes from 0 to 16 (try on the Go Playground ):
var imgdata = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
Saving as string base64
If the file is not “too large” (most of the images / icons meet the requirements), there are other viable options. You can convert the contents of the file to a string Base64 and save it in the source code. When you run the application ( func init() ) or if necessary, you can decode it into the original []byte content. Go has good support for Base64 encoding in the encoding/base64 package.
Converting a (binary) file to a base64 string is as simple as:
data, err := ioutil.ReadFile("someimage.png") if err != nil { panic(err) } fmt.Println(base64.StdEncoding.EncodeToString(data))
Save the base64 result string in your source code, for example, as const .
Decryption is just one function call:
const imgBase64 = "<insert base64 string here>" data, err := base64.StdEncoding.DecodeString(imgBase64) // data is of type []byte
Query string storage
More efficient than storing as base64, but it may be longer in the source code to store a string literal in binary quotation marks. We can get any string in quotes using strconv.Quote() :
data, err := ioutil.ReadFile("someimage.png") if err != nil { panic(err) } fmt.Println(strconv.Quote(string(data))
For binary data containing values from 0 to 64, here is what the output will look like (try on the Go Playground ):
"\x00\x01\x02\x03\x04\x05\x06\a\b\t\n\v\f\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f ! \"#$%&'()*+,-./0123456789:;<=>?"
(Note that strconv.Quote() appends and appends a quotation mark to it.)
You can directly use this string in quotation marks in your source code, for example:
const imgdata = "\x00\x01\x02\x03\x04\x05\x06\a\b\t\n\v\f\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&'()*+,-./0123456789:;<=>?"
It is ready to use; there is no need to decode it; citation cancellation is done by the Go compiler at compile time.
You can also save it as a piece of byte if you need it:
var imgdata = []byte("\x00\x01\x02\x03\x04\x05\x06\a\b\t\n\v\f\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&'()*+,-./0123456789:;<=>?")