日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 go 删除 文件 某行_Go实战--用echo嵌入静态资源 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

生命不止,繼續 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-syso1go 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嵌入静态资源的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。