☰ Menu
Home›Command-line Tools›go-statik
CLI Open source 2 installs/mo

go-statik for Mac

go-statik

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

  1. Open Terminal (press ⌘ + Space and type “Terminal”).
  2. If Homebrew is not installed yet, run the command below first.
  3. Run this command:

Latest 10 releases

Last updated 2026-01-10; current version v0.1.8, with the latest 7 releases listed.

VersionReleasedTypeSizemacOSWindowsLinux
v0.1.82026-01-10Stable————
v0.1.72020-02-25Stable————
v0.1.62019-03-20Stable————
v0.1.52018-09-21Stable————
v0.1.42018-08-21Stable————
v0.1.32018-08-03Stable————
v0.1.22018-07-09Stable————

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.

Compared with similar apps
AppVersionLicenseArchitecture30-day installs
go-statikv0.1.8Apache-2.0CLI2
stuffbin1.3.0MITCLI3
go-rice1.0.3BSD-2-ClauseCLI0
execstack20130503GPL-2.0-or-laterCLI14

Similar apps: Command-line Tools

Leave a comment

评论表单加载中…