生命不止,继续 go go go !!!

使用 Go 开发应用的时候,有时会遇到需要读取静态资源的情况。比如开发 Web 应用,程序需要加载模板文件生成输出的 HTML。在程序部署的时候,除了发布应用可执行文件外,还需要发布依赖的静态资源文件。这给发布过程添加了一些麻烦。既然发布单独一个可执行文件是非常简单的操作,就有人会想办法把静态资源文件打包进 Go 的程序文件中。

参考地址:
http://fuxiaohei.me/2016/10/1/go-binary-embed-asset.html

文中提到了:
go-bindata
go.rice
esc

本片博客只会介绍go.rice,其余的会之后进行介绍的。

What’s an Embedded Resource?
An embedded resource in a application is a file that is included as part of the application. The file is not compiled, but is accessable from the code at run-time. Embedded resources can be any file type.

Languages as JAVA and C# support resources out of box. However, this is not the case for Golang. In order to emebed resource, we need to develop our own solution. Thankfully, there are couple of tools that are doing this for us.

参考地址:
http://blog.ralch.com/tutorial/golang-embedded-resources/

go.rice

go.rice is a Go package that makes working with resources such as html,js,css,images,templates, etc very easy.

github地址:
https://github.com/GeertJohan/go.rice

Star: 1107

获取:

go get github.com/GeertJohan/go.ricego get github.com/GeertJohan/go.rice/rice12

FindBox
funcation to access a particular resource bundler (directory).
The function is finding the correct absolute path for your resource files.

// find a rice.BoxtemplateBox, err := rice.FindBox("your-resource-directory")if err != nil {    log.Fatal(err)}// get file contents as stringtmpl, err := templateBox.String("your_asset.tmpl")if err != nil {    log.Fatal(err)}12345678910

Embedded resource as source code
作为源码嵌入资源
命令:

rice embed-go1

生成文件:

.rice-box.go1

Embedded resource as an archive
appends a resource as a zip file to already built executable
以zip的形式附加到已经存在的可执行文件

Embedded resource as an syso resource
This is experimental method that generates .syso file that is compiled by Go compiler. The following command generates the coff syso resource files per directory:

rice embed-syso1
go build -o rice append --exec 12

echo中使用go.rice

代码main.go:

package mainimport (    "net/http"    "github.com/GeertJohan/go.rice"    "github.com/labstack/echo")func main() {    e := echo.New()    // the file server for rice. "app" is the folder where the files come from.    assetHandler := http.FileServer(rice.MustFindBox("app").HTTPBox())    // serves the index.html from rice    e.GET("/", echo.WrapHandler(assetHandler))    // servers other static files    e.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", assetHandler)))    e.Logger.Fatal(e.Start(":1323"))}12345678910111213141516171819202122

跟main.go同一级,新建一个文件夹app,放入文件file.txt

执行:

rice embed-go1

生成了 rice-box.go:

package mainimport (    "github.com/GeertJohan/go.rice/embedded"    "time")func init() {    // define files    file2 := &embedded.EmbeddedFile{        Filename:    "file.txt",        FileModTime: time.Unix(1511406219, 0),        Content:     string(""),    }    // define dirs    dir1 := &embedded.EmbeddedDir{        Filename:   "",        DirModTime: time.Unix(1511406219, 0),        ChildFiles: []*embedded.EmbeddedFile{            file2, // "file.txt"        },    }    // link ChildDirs    dir1.ChildDirs = []*embedded.EmbeddedDir{}    // register embeddedBox    embedded.RegisterEmbeddedBox(`app`, &embedded.EmbeddedBox{        Name: `app`,        Time: time.Unix(1511406219, 0),        Dirs: map[string]*embedded.EmbeddedDir{            "": dir1,        },        Files: map[string]*embedded.EmbeddedFile{            "file.txt": file2,        },    })}123456789101112131415161718192021222324252627282930313233343536373839404142

执行:

go build1

生成文件:embed_resources.exe

运行embed_resources.exe
删除app文件夹下的file.txt
浏览器访问http://localhost:1323/
可以看到file.txt文件

go 删除 文件 某行_Go实战--用echo嵌入静态资源相关推荐

  1. linux删除文件_【Linux实战】Vim编辑器和恢复ext4下误删除的文件

    学神IT教育:XueGod-IT 良心教育 贴心服务 1.  vim主要模式介绍,vim命令模式. 确保系统已经安装了VIM工具 [root@panda ~]# rpm -qf `which vim` ...

  2. linux创建删除文件命令行,彻底消失,Linux下用命令行彻底删除文件

    在大多数情况下,我们想要删除电脑文件,Linux下一般会使用删除键或者rm命令.将文件扔到垃圾箱.但是,这些方法并没把文件从我们的电脑里彻底删除,只是把文件隐藏起来,让用户看不见而已. 假设某个文件含 ...

  3. python删除文件指定行

    我们现在有五行数据,我们想删除第三行:pig 删除文本指定行: 删除文本指定行用的是for i in i,找到指定行的关键字,将不包括关键字的其他行放在生成器中,将生成器包含的内容重新写入文件. 我们 ...

  4. python 删除文件首行或指定行(修改文件)

    清除文件内容 使用情况:在对文件写入内容时,在某个条件下要清除文件内容再进行写入操作时,就可以使用truncate(size),不用删除文件再从新创建. 我们可以使用 truncate(size)函数 ...

  5. linux 删除文件第一行的方法

    删除第一行 sed -i '1d' filename 范围删除,删除1-3行 sed -i '1,3d' filename 删除第n行 sed -i 'nd' filename 删除最后一行 sed ...

  6. Linux之创建、删除文件及目录命令touch、echo、mkdir、rm、rmdir

    1. 创建.删除文件及目录命令的使用 命令 说明 touch 文件名    [可以没有后缀,默认为文本文件(没有后缀)] 创建指定文件 mkdir 目录名 创建目录(文件夹) rm 文件名或者目录名 ...

  7. Hexo文件压缩:使用hexo-neat插件压缩页面静态资源

    目录 为什么要压缩页面静态资源 hexo的压缩静态资源插件 如何使用hexo-neat 在站点根目录下安装hexo-neat 为站点配置文件添加相关配置 hexo-neat插件踩坑记录 跳过压缩文件的 ...

  8. Linux sed删除文件注释行并删除空行

    #删除注释行 $ sed '/^#/d' <file> $ sed '/#/d' <file># 删除空行 $ sed '/^$/d' <file> # 删除空行并 ...

  9. python删除文件某行_python 文件操作删除某行的实例

    python 文件操作删除某行的实例 使用continue跳过本次写循环就可以了 #文本内容 Yesterday when I was young 昨日当我年少轻狂 The tasting of li ...

最新文章

  1. Android RxJava 2.0中backpressure(背压)概念的理解
  2. PCB 围绕CAM自动化,打造PCB规则引擎
  3. error: #5: cannot open source input file core_cm3.h: No such file or directory
  4. jQuery的入口函数
  5. C语言打印输出红色字体
  6. 《Windows服务器配置与管理》远程桌面管理
  7. 谨防 ActiveSupport::Cache::Store 缓存 nil 值
  8. 精通Server 2008多元密码策略之PowerShell篇
  9. ArcMAP问题集锦(一)
  10. kafka 下载与命令的使用
  11. Excel-每隔几行进行转置一次
  12. linux apache 503,Apache ProxyPass出现503 Service Temporarily Unavailable错误
  13. 你拍一我拍一上学得学计算机,儿歌你拍一我拍一
  14. Kubernetes (K8s) 安装部署过程(七)之部署node节点
  15. ORB-SLAM2的源码阅读(九):Initializer类
  16. 全新的 Uber 应用设计
  17. 辞职信微信html,微信退款处理.html
  18. 宏 word 更新域
  19. tbb学习笔记(一): tbb容器及Mutex
  20. R语言之plot()画图

热门文章

  1. java 将json转成utf 8_解决JSON.stringify()自动将中文转译成unicode的方法
  2. 添加多个tomcat服务目录
  3. Android 串口开发,发送串口命令,读卡,反扫码,USB通讯,实现demo。——持续更新
  4. Android 自定义带图标Toast,工具方法,Toast自定义显示时间
  5. hapi 使用 lab 和 code 进行测试
  6. c# oracle datasource,C# 连接Oracle 数据库 示例源码下载
  7. Thymeleaf视图
  8. activiti5.22 springboot 流程引擎 实战全过程
  9. catia钣金根据线段折弯_折弯大神分析钣金折弯下刀顺序
  10. (二)ElasticSearch6.1.1 Python API