Go 1.16 embed特性的简单应用

项目结构如下:

1└─ui 2 └─embed_ui.go 3 └─dist 4 └─index.html 5 └─static 6 ├─css 7 └─ ... 8 ├─fonts 9 └─ ... 10 └─js 11 └─ ... 12└─main.go 13└─go.mod 14 15 16//embed_ui.go 17package ui 18 19import ( 20 `embed` 21) 22 23//go:embed dist 24var WebUI embed.FS 25 26 27//main.go 28// 嵌入普通静态资源 29type StaticResource struct { 30 // 静态资源 31 staticFS embed.FS 32 // 设置embed文件到静态资源的相对路径,也就是embed注释里的路径 33 path string 34} 35// 静态资源被访问逻辑 36func (_this_ *StaticResource) Open(name string) (fs.File, error) { 37 var fullName string 38 if strings.Contains(name,`/`){ 39 fullName = path.Join(_this_.path,"static",name) 40 }else{ 41 fullName = path.Join(_this_.path,name) 42 } 43 file, err := _this_.staticFS.Open(fullName) 44 return file, err 45} 46 47func main() { 48 // 设置静态资源 49 static := &StaticResource{ 50 staticFS: ui.WebUI, 51 path: "dist", 52 } 53 54 engine := gin.Default() 55 { 56 // 设置 57 engine.StaticFS("/static/",http.FS(static)) 58 // 首页 59 engine.GET("/", func(context *gin.Context) { 60 context.Writer.WriteHeader(http.StatusOK) 61 indexHTML,_ := static.staticFS.ReadFile(static.path + "/" + "index.html") 62 context.Writer.Write(indexHTML) 63 context.Writer.Header().Add("Accept","text/html") 64 context.Writer.Flush() 65 }) 66 } 67 engine.Run() 68} 69 70 71[GIN-debug] GET /static/*filepath --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers) 72[GIN-debug] HEAD /static/*filepath --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers) 73[GIN-debug] GET / --> main.main.func1 (3 handlers) 74[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default 75[GIN-debug] Listening and serving HTTP on :8080 76[GIN] 2021/02/17 - 17:37:07 | 200 | 530.8µs | ::1 | GET "/" 77[GIN] 2021/02/17 - 17:37:07 | 200 | 135.1147ms | ::1 | GET "/static/js/chunk-2d0b6337.40e74af5.js" 78[GIN] 2021/02/17 - 17:37:07 | 200 | 134.5926ms | ::1 | GET "/static/css/chunk-3d4a32e8.4951a1b7.css" 79[GIN] 2021/02/17 - 17:37:07 | 200 | 146.7519ms | ::1 | GET "/static/css/index.d72cf005.css" 80[GIN] 2021/02/17 - 17:37:07 | 200 | 147.2865ms | ::1 | GET "/static/js/index.15d7bf17.js" 81[GIN] 2021/02/17 - 17:37:07 | 200 | 151.6588ms | ::1 | GET "/static/css/chunk-vendors.16da611a.css" 82[GIN] 2021/02/17 - 17:37:07 | 200 | 148.8884ms | ::1 | GET "/static/js/chunk-vendors.24c0b194.js" 83[GIN] 2021/02/17 - 17:37:07 | 200 | 0s | ::1 | GET "/static/js/chunk-2d0d69a3.6eb93f6e.js" 84[GIN] 2021/02/17 - 17:37:07 | 200 | 364.8µs | ::1 | GET "/static/js/chunk-2d0e53c4.94fb2765.js" 85[GIN] 2021/02/17 - 17:37:07 | 200 | 382.2µs | ::1 | GET "/static/js/chunk-3d4a32e8.ced07e34.js" 86[GIN] 2021/02/17 - 17:37:07 | 200 | 0s | ::1 | GET "/static/fonts/element-icons.535877f5.woff"

不太习惯写长篇大论的文章,看得不是太明白的可以参考下面的链接,研究这个也是了解下用法,如果有更好的写法欢迎留言,共同讨论

参考: https://blog.csdn.net/RA681t58CJxsgCkJ31/article/details/113777516

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang

Go 1.16新功能特性不完全前瞻

Go1.16将于2021年2月发布。目前已经进入freeze状态,即不再接受新feature,仅fixbug、编写文档和接受安全更新等。目前Go1.16的发布说明尚处于早期草稿阶段,但Go团队成员正在致力于编写发布说明。Go1.16的完全特性列表说明还得等真正发布前才能得到。如今要了解Go1.16功能特性都有哪些变化,只能结合现有的releas