go-rice for Mac
go.rice is a Go package that makes working with resources such as html,js,css,images and templates easy. During development go.rice will load required files directly from disk.
go-rice — overview
go.rice
go.rice is a Go package that makes working with resources such as html,js,css,images and templates easy. During development go.rice will load required files directly from disk. Upon deployment it's easy to add all resource files to a executable using the rice tool, without changing the source code for your package. go.rice provides methods to add resources to a binary in different scenarios.
What does it do
The first thing go.rice does is finding the correct absolute path for your resource files. Say you are executing a binary in your home directory, but your html-files are in $GOPATH/src/yourApplication/html-files. go.rice will lookup the correct path for that directory (relative to the location of yourApplication). All you have to do is include the resources using rice.FindBox("html-files").
This works fine when the source is available to the machine executing the binary, which is the case when installing the executable with go get or go install. But it does not work when you wish to provide a single binary without source. This is where the rice tool comes in. It analyses source code and finds call's to rice.FindBox(..). Then it adds the required directories to the executable binary, There are two strategies to do this. You can 'embed' the assets by generating go source code and then compile them into the executable binary, or you can 'append' the assets to the executable binary after compiling. In both cases the rice.FindBox(..) call detects the embedded or appended resources and load those, instead of looking up files from disk.
Installation
Use go install to install the package the rice tool.
Package usage
Import the package: import "github.com/GeertJohan/go.rice"
Serving a static content folder over HTTP with a rice Box:
Serve a static content folder over HTTP at a non-root location:
Note the trailing slash in /css/ in both the call to http.StripPrefix and http.Handle.
Loading a template:
Never call FindBox() or MustFindBox() from an init() function, as there is no guarantee the boxes are loaded at that time.
Calling FindBox and MustFindBox
Always call FindBox() or MustFindBox() with string literals e.g. FindBox("example"). Do not use string constants or variables. This will prevent the rice tool to fail with error Error: found call to rice.FindBox, but argument must be a string literal..
Tool usage
The rice tool lets you add the resources to a binary executable so the files are not loaded from the filesystem anymore. This creates a 'standalone' executable. There are multiple strategies to add the resources and assets to a binary, each has pro's and con's but all will work without requiring changes to the way you load the resources.
rice embed-go: Embed resources by generating Go source code
Execute this method before building. It generates a single Go source file called rice-box.go for each package. The generated go file contains all assets. The Go tool compiles this into the binary.
Details
轻松在 Go 中嵌入 HTML、JS、CSS、图片和模板等资源
How to install go-rice
- Open Terminal (press ⌘ + Space and type “Terminal”).
- If Homebrew is not installed yet, run the command below first.
- Run this command:
Latest 10 releases
This project has no GitHub releases yet.
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
- Apple silicon · macOS 13 Ventura
- Intel · macOS 14 Sonoma
- Intel · macOS 13 Ventura
- 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-rice | 1.0.3 | BSD-2-Clause | CLI | 0 |
| libhubbub | 0.3.8 | MIT | CLI | 0 |
| confd | v0.16.0 | MIT | CLI | 2 |
| htmlcompressor | 1.5.3 | Apache-2.0 | CLI | 1 |
Similar apps: Command-line Tools
libhubbub
libhubbub is an HTML parser library designed primarily to parse the markup structure of web…confd
confd is a lightweight configuration management tool focused on:htmlcompressor
htmlcompressor is a command-line tool designed primarily for compressing HTML or XML files.imgdiet
imgdiet is a command-line image processing tool primarily used for compressing and resizing…vipsdisp
Vipsdisp can display huge (many, many gigabyte) images quickly and without using much memory.betty
Betty is a friendly English-like interface for your command line.Categories: Command-line Tools