go-statik for Mac
statik allows you to embed a directory of static files into your Go binary to be later served from an http.FileSystem.
go-statik — overview
statik
statik allows you to embed a directory of static files into your Go binary to be later served from an http.FileSystem.
Is this a crazy idea? No, not necessarily. If you're building a tool that has a Web component, you typically want to serve some images, CSS and JavaScript. You like the comfort of distributing a single binary, so you don't want to mess with deploying them elsewhere. If your static files are not large in size and will be browsed by a few people, statik is a solution you are looking for.
Usage
Install the command line tool first.
go install github.com/rakyll/statik@latest
statik is a tiny program that reads a directory and generates a source file that contains its contents. The generated source file registers the directory contents to be used by statik file system.
The command below will walk on the public path and generate a package called statik under the current working directory.
$ statik -src=/path/to/your/project/public
The command below will filter only files on listed extensions.
$ statik -include=.jpg,.txt,.html,.css,.js
In your program, all your need to do is to import the generated package, initialize a new statik file system and serve.
~~~ go import ( "github.com/rakyll/statik/fs"
_ "./statik" // TODO: Replace with the absolute import path )
// ...
statikFS, err := fs.New() if err != nil { log.Fatal(err) }
// Serve the contents over HTTP. http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(statikFS))) http.ListenAndServe(":8080", nil) ~~~
Visit http://localhost:8080/public/path/to/file to see your file.
You can also read the content of a single file:
~~~ go import ( "io"
"github.com/rakyll/statik/fs"
_ "./statik" // TODO: Replace with the absolute import path )
// ...
statikFS, err := fs.New() if err != nil { log.Fatal(err) }
// Access individual files by their paths. r, err := statikFS.Open("/hello.txt") if err != nil { log.Fatal(err) } defer r.Close() contents, err := io.ReadAll(r) if err != nil { log.Fatal(err) }
fmt.Println(string(contents)) ~~~
There is also a working example under example directory, follow the instructions to build and run it.
Note: The idea and the implementation are hijacked from camlistore. I decided to decouple it from its codebase due to the fact I'm actively in need of a similar solution for many of my projects.
Deterministic output
By default, statik includes the "last modified" (mtime) time on files that it packs. This allows an HTTP FileServer to present the correct file modification times to clients.
However, if you have a continuous integration task that checks that your checked-in static files in a git repository match the code that is generated on your CI system, you'll run into a problem: The mtime on the git checkout does not match what you have locally, causing tests to fail.
Details
将文件嵌入到 Go 可执行文件中
How to install go-statik
- Open Terminal (press ⌘ + Space and type “Terminal”).
- If Homebrew is not installed yet, run the command below first.
- Run this command:
Latest 10 releases
Last updated 2026-01-10; current version v0.1.8, with the latest 7 releases listed.
| Version | Released | Type | Size | macOS | Windows | Linux |
|---|---|---|---|---|---|---|
| v0.1.8 | 2026-01-10 | Stable | — | — | — | — |
| v0.1.7 | 2020-02-25 | Stable | — | — | — | — |
| v0.1.6 | 2019-03-20 | Stable | — | — | — | — |
| v0.1.5 | 2018-09-21 | Stable | — | — | — | — |
| v0.1.4 | 2018-08-21 | Stable | — | — | — | — |
| v0.1.3 | 2018-08-03 | Stable | — | — | — | — |
| v0.1.2 | 2018-07-09 | Stable | — | — | — | — |
Data from GitHub Releases; stable releases first, each group sorted newest first.
Prebuilt bottle platforms
Official Homebrew bottles cover:
- Apple silicon · latest macOS
- Apple silicon · macOS 26 Tahoe
- Apple silicon · macOS 15 Sequoia
- Apple silicon · macOS 14 Sonoma
- Intel · macOS 14 Sonoma
- Linux · ARM64
- Linux · x86_64
Compared with similar apps
Only public metadata is compared, with no subjective recommendation; see the compare pages for field-by-field comparisons.
| App | Version | License | Architecture | 30-day installs |
|---|---|---|---|---|
| go-statik | v0.1.8 | Apache-2.0 | CLI | 2 |
| stuffbin | 1.3.0 | MIT | CLI | 3 |
| go-rice | 1.0.3 | BSD-2-Clause | CLI | 0 |
| execstack | 20130503 | GPL-2.0-or-later | CLI | 14 |
Similar apps: Command-line Tools
stuffbin
stuffbin is a utility + package to compress and embed static files and assets into Go binaries…go-rice
go.rice is a Go package that makes working with resources such as html,js,css,images and…execstack
execstack is a small command-line utility used to set, clear, or query the executable stack bit…lief
Library to Instrument Executable Formatscargo-bloat
Find out what takes most of the space in your executable.osm-gps-map
osm-gps-map is a Gtk mapping widget (and Python bindings) that when given GPS co-ordinates…Categories: Command-line Tools