當使用golang開發一個網站時 , 一般是需要部署下靜態檔案的資源 ,go只是提供介面 , 打包完的go二進制檔案不包含靜態檔案資源
但是當做一個小型的應用時還要再部署靜態資源顯得比較麻煩 , 這個時候可以使用packr這個包來把靜態資源直接打包進二進制里
包的github地址是 https://github.com/gobuffalo/packr
使用的時候可以直接在go.mod里使用v2版本就可以了
go.mod
github.com/gobuffalo/packr/v2 v2.5.1
呼叫的時候使用import引入
import (
"github.com/gobuffalo/packr/v2"
)
具體方法可以參照我這個 , 也可以去看下檔案
//獲取檔案內容,可以打包到二進制
func FileGetContent(file string) string {
str := ""
box := packr.New("tmpl","../static")
content, err := box.FindString(file)
if err != nil {
return str
}
return content
}
重點是在最終打包build的時候 ,先使用packr2 build 以下 , 然后再去go build
packr2命令需要單獨下載
https://github.com/gobuffalo/packr/releases

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/5028.html
標籤:Go
下一篇:Go 其二 陣列與切片
