Embedding static files in a go binary using go embed
source link: https://harsimranmaan.medium.com/embedding-static-files-in-a-go-binary-using-go-embed-bac505f3cb9a
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Embedding static files in a go binary using go embed
Starting with Golang 1.16, file embedding is supported in Go without the need of an external package. I have been using packages like pkgr and packr to make it easier to embed static files in go binaries. We’ll explore the usage of the new //go:embed
directive in this post.
Why do we need static file embedding?
There would be cases when we’d want to embed static files in the binary. While it is possible to build a binary that reads static files off the disk, it is often cumbersome to ship it to end users as the static files have to be located at a certain place on user’s device or the location has to be configurable. On top of that, the user must remember to update all the files/config refs when upgrading or moving the binary to a new location.
Here are some common use cases where static file embedding is often used:
Go templates: The template files need to be available to the binary. This is a fairly common use case for web server binaries or CLI applications offering an init
command which creates a sample project. In absence of embedding, the templates are often inlined in the code. eg: See qbec init or the source.
Static web servers: Sometimes static files like an index.html
or other HTML, JavaScript and CSS files need to be shipped with a golang server binary so that the user can run the server and serve these files. eg: Static file embedding in a web server
Database migrations: Another place where embedded files can be used is to ship database migration scripts with the binary. eg: Database migration files
Using the go embed directive
The first thing you need to use the embed directive is to make sure that you are using at-least golang 1.16. For older versions of Go, consider using pkgr.
We’ll write an application that runs a web app which echoes the url back in a webpage (Fancy!! isn’t it?)
The program reads the web page title from a static title.txt
file, serves static files (css/main.css
) over the /static/
route and uses a go template index.html.tmpl
to render all routes over HTTP port 3000. All static files and templates are embedded in the binary using the new embed directive.
Project structure:
.
├── go.mod
├── main.go
├── static
│ └── css
│ └── main.css
├── templates
│ └── index.html.tmpl
└── title.txt
Let’s build and deploy the web server.
$ go version
go version go1.16 darwin/arm64$ go build . # Build the binary$ cp ./go-embed /tmp/ # copy to temp directory. This would help validate that static files are actually embedded$ cd /tmp && ./go-embed # Run the server2020/12/18 12:21:15 Listening on :3000...
2020/12/18 12:21:23 Serving request for path /some-path-with-css-applied
2020/12/18 12:21:23 Serving request for path /favicon.ico
2020/12/18 12:21:24 Serving request for path /some-path-with-css-applied
2020/12/18 12:21:24 Serving request for path /favicon.ico
2020/12/18 12:21:25 Serving request for path /some-path-with-css-applied
2020/12/18 12:21:25 Serving request for path /favicon.ico
Browse to http://localhost:3000/
The final result is a single web server binary ready for distribution. For more information check out the official embed documentation. Happy embedding.
The source code for the server is available at
Recommend
-
21
The Ape Programming Language About Ape is an easy to use programming language and library written in C. It's an offspring of Monkey language (from
-
32
You have a blob of some data which you would like to embed into your C program. Perhaps a splash screen, or a special font, firmware for your scsi card, or whatever. The usual approach which I think most people are famili...
-
1
Embedding media by its public URL using laravel-embed Monday, 24 A...
-
4
Embedding Google Forms in a Static Site When working on hobby projects, there’s something special about saving a few bucks by employing a DYI solution. Even when the trade-off is weeks worth of work to avoid a $5 per...
-
3
Embed Read-Only Files in Go Code May 03, 2018 The motivation is to embed da...
-
14
Go has this fantastic feature to embed parts of the filesystem directly into the binary. This example uses SvelteKit as an admin backend for a Go API. ...
-
2
PDF files are a very popular way of sharing formatted documents due to their flexibility and lightweight nature—they work on most devices and operating systems. In this post, I'll show you how to use a WordPress PDF plugin to embed and prot...
-
2
P1967 #embed and D2752 “Static storage for initializer_list” are now on Compil...
-
2
A Guide to Embedding Static Files in Go This is a simple Go server that serves files from an embed.FS file system. This is useful if you want to embed static files (like HTML, CSS, and JavaScript) directly into your Go binary. Usa...
-
6
A Guide to Embedding Static Files in Go using go:embedMar 21, 2023 • 4 min read • #cli...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK