之前簡單介紹過packr
,statik
等靜態資源嵌入工具包的使用,go.rich 是一個與packr 相似的靜態資源嵌入包,使用簡單
功能強大css
go mod init github.com/rongfengliang/rice-app
├── Makefile
├── README.md
├── go.mod
├── go.sum
├── http-files
│ ├── app.css
│ └── index.html
├── main.go
└── rice-box.go
package main
//go:generate go run github.com/GeertJohan/go.rice/rice embed-go
import (
"log"
"net/http"
rice "github.com/GeertJohan/go.rice"
)
func main() {
http.Handle("/", http.FileServer(rice.MustFindBox("http-files").HTTPBox()))
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatalln("some wrong will exit")
}
}
go.modhtml
module github.com/rongfengliang/rice-app
go 1.13
require github.com/GeertJohan/go.rice v1.0.0
Makefilelinux
build-app: clean make-dir generate build-mac build-linux build-windows
clean:
rm -rf build/* && rm -rf rice-box.go
generate:
go generate
make-dir:
mkdir -p build/{mac,linux,windows}
build-mac:
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o build/mac
build-linux:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/linux
build-windows:
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o build/windows
make
./build/mac/rice-app
效果git
go.rice 在功能上與packr很相似,都是一個很不錯的golang 靜態資源嵌入工具包github
https://github.com/GeertJohan/go.rice
https://github.com/rongfengliang/go-rice-learninggolang