您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Go 1.16 embed特性的簡單使用”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
項(xiàng)目結(jié)構(gòu)如下:
└─ui └─embed_ui.go └─dist └─index.html └─static ├─css └─ ... ├─fonts └─ ... └─js └─ ... └─main.go └─go.mod
//embed_ui.go package ui import ( `embed` ) //go:embed dist var WebUI embed.FS
//main.go // 嵌入普通靜態(tài)資源 type StaticResource struct { // 靜態(tài)資源 staticFS embed.FS // 設(shè)置embed文件到靜態(tài)資源的相對路徑,也就是embed注釋里的路徑 path string } // 靜態(tài)資源被訪問邏輯 func (_this_ *StaticResource) Open(name string) (fs.File, error) { var fullName string if strings.Contains(name,`/`){ fullName = path.Join(_this_.path,"static",name) }else{ fullName = path.Join(_this_.path,name) } file, err := _this_.staticFS.Open(fullName) return file, err } func main() { // 設(shè)置靜態(tài)資源 static := &StaticResource{ staticFS: ui.WebUI, path: "dist", } engine := gin.Default() { // 設(shè)置 engine.StaticFS("/static/",http.FS(static)) // 首頁 engine.GET("/", func(context *gin.Context) { context.Writer.WriteHeader(http.StatusOK) indexHTML,_ := static.staticFS.ReadFile(static.path + "/" + "index.html") context.Writer.Write(indexHTML) context.Writer.Header().Add("Accept","text/html") context.Writer.Flush() }) } engine.Run() }
[GIN-debug] GET /static/*filepath --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers) [GIN-debug] HEAD /static/*filepath --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers) [GIN-debug] GET / --> main.main.func1 (3 handlers) [GIN-debug] Environment variable PORT is undefined. Using port :8080 by default [GIN-debug] Listening and serving HTTP on :8080 [GIN] 2021/02/17 - 17:37:07 | 200 | 530.8μs | ::1 | GET "/" [GIN] 2021/02/17 - 17:37:07 | 200 | 135.1147ms | ::1 | GET "/static/js/chunk-2d0b6337.40e74af5.js" [GIN] 2021/02/17 - 17:37:07 | 200 | 134.5926ms | ::1 | GET "/static/css/chunk-3d4a32e8.4951a1b7.css" [GIN] 2021/02/17 - 17:37:07 | 200 | 146.7519ms | ::1 | GET "/static/css/index.d72cf005.css" [GIN] 2021/02/17 - 17:37:07 | 200 | 147.2865ms | ::1 | GET "/static/js/index.15d7bf17.js" [GIN] 2021/02/17 - 17:37:07 | 200 | 151.6588ms | ::1 | GET "/static/css/chunk-vendors.16da611a.css" [GIN] 2021/02/17 - 17:37:07 | 200 | 148.8884ms | ::1 | GET "/static/js/chunk-vendors.24c0b194.js" [GIN] 2021/02/17 - 17:37:07 | 200 | 0s | ::1 | GET "/static/js/chunk-2d0d69a3.6eb93f6e.js" [GIN] 2021/02/17 - 17:37:07 | 200 | 364.8μs | ::1 | GET "/static/js/chunk-2d0e53c4.94fb2765.js" [GIN] 2021/02/17 - 17:37:07 | 200 | 382.2μs | ::1 | GET "/static/js/chunk-3d4a32e8.ced07e34.js" [GIN] 2021/02/17 - 17:37:07 | 200 | 0s | ::1 | GET "/static/fonts/element-icons.535877f5.woff"
“Go 1.16 embed特性的簡單使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。