146

I'm working on a small web application in Go that's meant to be used as a tool on a developer's machine to help debug their applications/web services. The interface to the program is a web page that includes not only the HTML but some JavaScript (for functionality), images, and CSS (for styling). I'm planning on open-sourcing this application, so users should be able to run a Makefile, and all the resources will go where they need to go. However, I'd also like to be able to simply distribute an executable with as few files/dependencies as possible. Is there a good way to bundle the HTML/CSS/JS with the executable, so users only have to download and worry about one file?


Right now, in my app, serving a static file looks a little like this:

// called via http.ListenAndServe
func switchboard(w http.ResponseWriter, r *http.Request) {

    // snipped dynamic routing...

    // look for static resource
    uri := r.URL.RequestURI()
    if fp, err := os.Open("static" + uri); err == nil {
        defer fp.Close()
        staticHandler(w, r, fp)
        return
    }

    // snipped blackhole route
}
    

So it's pretty simple: if the requested file exists in my static directory, invoke the handler, which simply opens the file and tries to set a good Content-Type before serving. My thought was that there's no reason this needs to be based on the real filesystem: if there were compiled resources, I could simply index them by request URI and serve them as such.

Let me know if there's not a good way to do this or I'm barking up the wrong tree by trying to do this. I just figured the end-user would appreciate as few files as possible to manage.

2
  • See stackoverflow.com/a/12483181/993133
    – pd40
    Commented Dec 18, 2012 at 2:10
  • I actually just thought of exactly the same question today. The solution that I might explore is to use go generate with a small command-line utility (packaged with my source code) to convert the files into []byte slices that are embedded as variables in the code, similar to how stringer does it (see blog.golang.org/generate).
    – Ralph
    Commented Feb 14, 2017 at 12:50

5 Answers 5

128

Starting with Go 1.16 the go tool has support for embedding static files directly in the executable binary.

You have to import the embed package, and use the //go:embed directive to mark what files you want to embed and into which variable you want to store them.

3 ways to embed a hello.txt file into the executable:

import _ "embed"

//go:embed hello.txt
var s string
print(s)

//go:embed hello.txt
var b []byte
print(string(b))

//go:embed hello.txt
var f embed.FS
data, _ := f.ReadFile("hello.txt")
print(string(data))

Using the embed.FS type for the variable you can even include multiple files into a variable that will provide a simple file-system interface:

// content holds our static web server content.
//go:embed image/* template/*
//go:embed html/index.html
var content embed.FS

The net/http has support to serve files from a value of embed.FS using http.FS() like this:

http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(content))))

The template packages can also parse templates using text/template.ParseFS(), html/template.ParseFS() functions and text/template.Template.ParseFS(), html/template.Template.ParseFS() methods:

template.ParseFS(content, "*.tmpl")

The following of the answer lists your old options (prior to Go 1.16).


Embedding Text Files

If we're talking about text files, they can easily be embedded in the source code itself. Just use the back quotes to declare the string literal like this:

const html = `
<html>
<body>Example embedded HTML content.</body>
</html>
`

// Sending it:
w.Write([]byte(html))  // w is an io.Writer

Optimization tip:

Since most of the times you will only need to write the resource to an io.Writer, you can also store the result of a []byte conversion:

var html = []byte(`
<html><body>Example...</body></html>
`)

// Sending it:
w.Write(html)  // w is an io.Writer

Only thing you have to be careful about is that raw string literals cannot contain the back quote character (`). Raw string literals cannot contain sequences (unlike the interpreted string literals), so if the text you want to embed does contain back quotes, you have to break the raw string literal and concatenate back quotes as interpreted string literals, like in this example:

var html = `<p>This is a back quote followed by a dot: ` + "`" + `.</p>`

Performance is not affected, as these concatenations will be executed by the compiler.

Embedding Binary Files

Storing as a byte slice

For binary files (e.g. images) most compact (regarding the resulting native binary) and most efficient would be to have the content of the file as a []byte in your source code. This can be generated by 3rd party toos/libraries like go-bindata.

If you don't want to use a 3rd party library for this, here's a simple code snippet that reads a binary file, and outputs Go source code that declares a variable of type []byte that will be initialized with the exact content 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 would contain bytes from 0 to 16 (try it on the Go Playground):

var imgdata = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

Storing as base64 string

If the file is not "too large" (most images/icons qualify), there are other viable options too. You can convert the content of the file to a Base64 string and store that in your source code. On application startup (func init()) or when needed, you can decode it to the original []byte content. Go has nice support for Base64 encoding in the encoding/base64 package.

Converting a (binary) file to base64 string is as simple as:

data, err := ioutil.ReadFile("someimage.png")
if err != nil {
    panic(err)
}
fmt.Println(base64.StdEncoding.EncodeToString(data))

Store the result base64 string in your source code, e.g. as a const.

Decoding it is just one function call:

const imgBase64 = "<insert base64 string here>"

data, err := base64.StdEncoding.DecodeString(imgBase64) // data is of type []byte

Storing as quoted string

More efficient than storing as base64, but may be longer in source code is storing the quoted string literal of the binary data. We can obtain the quoted form of any string using the strconv.Quote() function:

data, err := ioutil.ReadFile("someimage.png")
if err != nil {
    panic(err)
}
fmt.Println(strconv.Quote(string(data))

For binary data containing values from 0 up to 64 this is how the output would look like (try it 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 prepends a quotation mark to it.)

You can directly use this quoted string 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, no need to decode it; the unquoting is done by the Go compiler, at compile time.

You may also store it as a byte slice should you need it like that:

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:;<=>?")
2
  • is there any way to bind an sh file to a go executable? Commented Nov 9, 2017 at 11:34
  • I guess data should be imgdata in the first code snippet under "storing as a byte slice" section. Commented Nov 21, 2017 at 17:55
81

The go-bindata package looks like it might be what you're interested in.

https://github.com/go-bindata/go-bindata

It will allow you to convert any static file into a function call that can be embedded in your code and will return a byte slice of the file content when called.

2
  • 8
    Upvoting this seems oddly self-serving in my case, but I'll do it anyway :p For the record though, it is not a package, but a command-line tool.
    – jimt
    Commented Dec 16, 2012 at 22:36
  • Just for the record, this is the path I took with my project. At some point @jimt introduced some new features to make things more user-friendly but no longer provided the granularity I needed, so I wrote my own tool which has less features but is designed for my usecase (I use this tool as sort of a preamble to the build process): github.com/jimmysawczuk/go-binary Commented Jan 21, 2015 at 17:03
8

Bundle React application

For example, you have a build output from react like the following:

build/favicon.ico
build/index.html
build/asset-manifest.json
build/static/css/**
build/static/js/**
build/manifest.json

When you use go:embed like this, it will serve the contents as http://localhost:port/build/index.html which is not what we want (unexpected /build).

//go:embed build/*
var static embed.FS

// ...
http.Handle("/", http.FileServer(http.FS(static)))

In fact, we will need to take one more step to make it works as expected by using fs.Sub:

package main

import (
    "embed"
    "io/fs"
    "log"
    "net/http"
)

//go:embed build/*
var static embed.FS

func main() {
    contentStatic, _ := fs.Sub(static, "build")
    http.Handle("/", http.FileServer(http.FS(contentStatic)))
    log.Fatal(http.ListenAndServe("localhost:8080", nil))
}

Now, http://localhost:8080 should serve your web application as expected.

Credit to Amit Mittal.

Note: go:embed requires go 1.16 or higher.

0
2

also there is some exotic way - I use maven plugin to build GoLang projects and it allows to use JCP preprocessor to embed binary blocks and text files into sources. In the case code just look like line below (and some example can be found here)

var imageArray = []uint8{/*$binfile("./image.png","uint8[]")$*/}
2
  • @is it possible to bind a directory having an sh or an executable like above Commented Nov 9, 2017 at 11:36
  • @KasunSiyambalapitiya Bind a directory? Bind an sh file? Not sure what you mean. If you want everything in a directory to be embedded, this is something I've done with go-bindata. For example, if I put //go:generate $GOPATH/bin/go-bindata -prefix=data/ -pkg=$GOPACKAGE data/ in a (non-generated) go file, go generate ./... will run go-bindata in the package's dir, embedding everything in a data subdir but with the 'data/' prefix removed.
    – Mark
    Commented Nov 26, 2017 at 5:16
1

As a popular alternative to go-bindata mentioned in another answer, mjibson/esc also embeds arbitrary files, but handles directory trees particularly conveniently.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.