Embedding assets into Go executable

The //go:embed feature added in Go 1.16 is pretty great, and this blog post keeps showing up on how to use it: How to Use //go:embed · The Ethically-Trained Programmer

I love how go generate can work together with //go:embed to simply the build workflows in a standardized way. Powerful, simple, and easy to understand!

This is a great way to embed a SPA web application into your Go executable.

1 Like

You can even embed assets into a library that can subsequently be imported! Pretty neat!

(I tested it) :slight_smile:

1 Like

Yes, this is super neat, because we can now commit embedded asset files directly, and they will get embedded at go build time instead of committing a large go module that contains embedded assets. In Simple IoT, we have refrained from committing the frontend assets (we’re still using Genesis), as it results in a 3.5MB Go module that bloats the repo, skews code metrics, etc. However, the JS module (compiled Elm) is only 161KB, so that seems a lot more reasonable. And we may be able to compress that more – if we xz the elm.js, it goes down to 43KB, and it would probably cost very little time to uncompress that in the HTTP api handler. Compiled Elm sizes are amazing.

This is a great idea! One could compress the embedded file using zlib or something, and then you can decompress it (exactly once) at app boot time using zlib package - compress/zlib - pkg.go.dev.