网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Go1.16embed特性的简单使用

本篇内容介绍了“Go 1.16 embed特性的简单使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

成都创新互联是一家以成都网站建设、网页设计、品牌设计、软件运维、成都网站营销、小程序App开发等移动开发为一体互联网公司。已累计为成都建筑动画等众行业中小客户提供优质的互联网建站和软件开发服务。

项目结构如下:

└─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
// 嵌入普通静态资源
type StaticResource struct {
	// 静态资源
	staticFS embed.FS
	// 设置embed文件到静态资源的相对路径,也就是embed注释里的路径
	path string
}
// 静态资源被访问逻辑
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() {
    // 设置静态资源
    static := &StaticResource{
        staticFS: ui.WebUI,
        path:     "dist",
    }

    engine := gin.Default()
    {
        // 设置
        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特性的简单使用

“Go 1.16 embed特性的简单使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


当前题目:Go1.16embed特性的简单使用
分享地址:http://bjjierui.cn/article/psjooj.html

其他资讯