The last time Hackerfall tried to access this page, it returned a not found error. A cached version of the page is below, or click here to continue anyway

Static files in Go

Static files in Go

Aug 2016

A common problem when developing a Go-based web service is that of static files. While you could rsync over your files before deploying a new binary, I prefer packing it into the binary for easier deployment and atomicity of the assets. There are quite a few packages out there already to help you compile assets into the binary, most notably the following:

None of these tools were quite there for me, and were missing some feature that I really wanted. The most important thing for me is that I wanted the assets to not just be compressed before being compiled into the binary, but to also be served compressed, so the server doesnt need to do an unnecessary decompression (and possibly compress it again before sending it to the client).


So, to scratch my own itch I made Staticfiles, a command line tool thats very similar to the tools listed above, but different in some key ways. Most notably, it doesnt constrain itself by implementing the http.FileSystem API, which makes it possible to do things like serving files directly in their compressed form. It also doesnt have a built-in development mode, but because of its support for build tags it is very easy to extend it with one (and I have an example of how to do this in the repo). The generated file server will add ETag and Last-Modified headers to the responses, to avoid unnecessary file downloads. I also made sure the tool itself is very fast, by parallelizing reading and compressing of files.

Check it out

Please try out Staticfiles and let me know what you think! If you run into a problem, feel free to open an issue and I can take a look.

You should follow me on Twitter!

Continue reading on