首页 > 其他 > 详细

go.rice 强大灵活的golang 静态资源嵌入包

时间:2019-11-05 17:43:18      阅读:343      评论:0      收藏:0      [点我收藏+]

以前简单介绍过packr ,statik 等静态资源嵌入工具包的使用,go.rich 是一个与packr 类似的静态资源嵌入包,使用简单
功能强大

项目结构

  • golang mod
 
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
  • 代码说明
    http-files 为静态资源的位置,我们可以通过代码直接引用,main.go 为入口,Makefile 为通过make 的跨平台编译处理
    rice-box.go 是使用go generate 生成的资源代码
    main.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.mod

module github.com/rongfengliang/rice-app
?
go 1.13
?
require github.com/GeertJohan/go.rice v1.0.0
 

Makefile

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 

效果

技术分享图片

 

 

说明

go.rice 在功能上与packr很类似,都是一个很不错的golang 静态资源嵌入工具包

参考资料

https://github.com/GeertJohan/go.rice
https://github.com/rongfengliang/go-rice-learning

go.rice 强大灵活的golang 静态资源嵌入包

原文:https://www.cnblogs.com/rongfengliang/p/11798991.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!