Go实战--golang中使用echo嵌入静态资源(labstackecho、GeertJohango.rice)

Go实战--golang中使用echo嵌入静态资源(labstackecho、GeertJohango.rice),第1张

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

生命不止,继续 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@H_301_40@/GeertJohan/go.rice@H_301_40@go get github.com@H_301_40@/GeertJohan/go.rice@H_301_40@/rice

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

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

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

rice embed-go@H_301_40@

生成文件:

<directory-name>.rice-Box.go@H_301_40@

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-syso@H_301_40@
go@H_301_40@ build -o <program>rice append@H_301_40@ --exec <program>
echo中使用go.rice

代码main.go:

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

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

执行:

rice embed-go@H_301_40@

生成了 rice-Box.go:

package@H_301_40@ mainimport@H_301_40@ (    "github.com/GeertJohan/go.rice/embedded"@H_301_40@    "time"@H_301_40@)func@H_301_40@ init() {    // define files@H_301_40@    file2 := &embedded.Embeddedfile{        filename:    "file.txt"@H_301_40@,fileModTime: time.Unix(1511406219@H_301_40@, 0@H_301_40@),Content:     string@H_301_40@(""@H_301_40@),}    // define dirs@H_301_40@    dir1 := &embedded.EmbeddedDir{        filename:   ""@H_301_40@,DirModTime: time.Unix(1511406219@H_301_40@,Childfiles: []*embedded.Embeddedfile{            file2,// "file.txt"@H_301_40@        },}    // link ChildDirs@H_301_40@    dir1.ChildDirs = []*embedded.EmbeddedDir{}    // register embeddedBox@H_301_40@    embedded.RegisterEmbeddedBox(`app`@H_301_40@,&embedded.EmbeddedBox{        name: `app`@H_301_40@,Time: time.Unix(1511406219@H_301_40@,Dirs: map@H_301_40@[string@H_301_40@]*embedded.EmbeddedDir{            ""@H_301_40@: dir1,},files: map@H_301_40@[string@H_301_40@]*embedded.Embeddedfile{            "file.txt"@H_301_40@: file2,})}

执行:

go@H_301_40@ build

生成文件:embed_resources.exe

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

总结

以上是内存溢出为你收集整理的Go实战--golang中使用echo嵌入静态资源(labstack/echo、GeertJohan/go.rice)全部内容,希望文章能够帮你解决Go实战--golang中使用echo嵌入静态资源(labstack/echo、GeertJohan/go.rice)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1273196.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存