zoukankan      html  css  js  c++  java
  • go.rice 强大灵活的golang 静态资源嵌入包

    以前简单介绍过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

  • 相关阅读:
    11月2日
    Rain和小爱的幸福山洞
    乍冷初寒
    支付宝接口源代码
    因没有设置文件夹权限导致的发布的页面不能在文本中写人数据
    asp.net真的是并行处理request的吗?
    Javascript学习笔记
    .net Windows服务程序和安装程序制作图解
    String.Trim()真相大揭秘
    c#基础知识总结学习
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/11798991.html
Copyright © 2011-2022 走看看