gin: 打包模板文件、静态文件到二进制文件中

一,默认html模板不会打包到二进制文件中

如果二进制文件的当前目录下不包含html模板文件路径,会报错如下

$ ./mediabank 
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.- using env:   export GIN_MODE=release- using code:  gin.SetMode(gin.ReleaseMode)[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)
panic: html/template: pattern matches no files: `templates/**/*.html`goroutine 1 [running]:
html/template.Must(...)/usr/local/soft/go/src/html/template/template.go:368
github.com/gin-gonic/gin.(*Engine).LoadHTMLGlob(0xc0001c3ba0, {0xa8207b, 0x13})/data/gopath/pkg/mod/github.com/gin-gonic/gin@v1.10.1/gin.go:263 +0x2f2
mediabank/routes.Routes()/data/goapp/mediabank/routes/routes.go:15 +0x5b
main.main()/data/goapp/mediabank/main.go:10 +0x13

二,用打包到二进制文件中

代码

main

package mainimport ("embed""mediabank/routes"
)// 嵌入文件只能为源码文件同级目录和子目录下的文件
//go:embed static/* templates/*
var embedFs embed.FS// 入口函数
func main() {//引入路由r := routes.Routes(embedFs)//runr.Run(":8080")
}

route

package routesimport ("embed""github.com/gin-gonic/gin""html/template""io/fs""mediabank/controller""net/http"
)func Routes(embedFs embed.FS) *gin.Engine {router := gin.Default()//1, 加载模板文件tmpl := template.Must(template.New("").ParseFS(embedFs, "templates/**/*.html"))router.SetHTMLTemplate(tmpl)//2, 加载静态文件fp, _ := fs.Sub(embedFs, "static")router.StaticFS("/static", http.FS(fp))//3,加载favicon//static是存放favicon.ico的目录faviconHandler := http.FileServer(http.FS(fp))router.GET("/favicon.ico", func(c *gin.Context) {faviconHandler.ServeHTTP(c.Writer, c.Request)})//mediamedia := controller.NewMediaController()mediaGroup := router.Group("/media"){mediaGroup.GET("/detail", media.Detail)mediaGroup.GET("/list", media.List)mediaGroup.GET("/user", media.User)}return router
}

三,运行结果:

$ ./mediabank 
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.- using env:   export GIN_MODE=release- using code:  gin.SetMode(gin.ReleaseMode)[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    /favicon.ico              --> mediabank/routes.Routes.func1 (3 handlers)
[GIN-debug] GET    /media/detail             --> mediabank/controller.(*MediaController).Detail-fm (3 handlers)
[GIN-debug] GET    /media/list               --> mediabank/controller.(*MediaController).List-fm (3 handlers)
[GIN-debug] GET    /media/user               --> mediabank/controller.(*MediaController).User-fm (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on :8080

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/908237.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

gin: 判断是否ajax请求

一,代码 controller: //得到用户信息 func (ic *MediaController) User(c *gin.Context) {if c.Request.Header.Get("X-Requested-With") == "XMLHttpRequest" {c.JSON(http.StatusOK, gin.H{&qu…

gin: 静态文件

一,下载jquery 官网: https://jquery.com/ 从命令行下载: $ wget https://code.jquery.com/jquery-3.7.1.min.js 二,代码: 目录结构routes package routesimport ("github.com/gin-gonic/gin""mediab…

详细介绍:【论文精读】基于YOLOv3算法的高速公路火灾检测

详细介绍:【论文精读】基于YOLOv3算法的高速公路火灾检测pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consola…

产品设计

产品设计2025-09-20 在设计tab时,如果数据没有加载出来,要么不切tab高亮,如果想切tab高亮,就先显示空白(因为还没拿到数据),或者旧的数据(依据产品性质)。 为了避免因为网络卡,或者没卡,没找到数据时,tab切…

An Empirical Study on Commit Message Generation using LLMs via In-Context Learning 论文笔记

介绍 (1) 发表:ICSE25 (2) 背景 最近的一些工作研究了基于 LLM 的提交信息生成,然而目前尚不清楚 LLM 通过 ICL 在该领域的表现如何 (3) 贡献 本文对通过 ICL 使用 LLM 进行提交信息生成进行了实证研究,并创建了一个…

实用指南:人工智能学习:Transformer结构中的编码器层(Encoder Layer)

实用指南:人工智能学习:Transformer结构中的编码器层(Encoder Layer)pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family:…

vcpkg 安装依赖

vcpkg install nlohmann-json

Java03课前问题列表

Java课前问题列表031.什么样的方法应该用static修饰?不用static修饰的方法往往具有什么特性?Student的getName应该用static修饰吗?不依赖于对象状态的方法、工具方法、工厂方法(用于创建对象)、主方法 main。 特性…

JavaScript错误处理完全指南:从基础到自定义错误实战

本手册深入讲解JavaScript错误处理机制,涵盖错误类型、try/catch/finally使用、自定义错误创建及实际应用场景,帮助开发者编写更健壮的代码。JavaScript错误处理手册 错误和异常在应用程序开发中是不可避免的。作为程…

1、论文准备

1、论文准备顺序2、项目要求 (AI智能评审) 3、论文字数要求4、论文不需要写题目 5、论文工期及金额6、记论文 7、机考打字格式 以上仅供参考,如有疑问,留言联系

Jetpack Navigation - 在 Fragment 中跳转到 Activity(4 种方式) - 详解

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

PION 游击

Day -70 只有 \(70\) 天了,是时候开坑了。 做昨天模拟赛的 T4,\(n=T=3\times 10^4\) 开了 \(2s\)。 感觉 \(O(Tn)\) 可以争一下,在 CF 的原上面过了。 可爱的搬题人,CF 上面只有 \(2\times 10^4\) 而且开了 \(7s\)…

神经网络构成框架-理论学习 - 指南

神经网络构成框架-理论学习 - 指南2025-09-20 09:24 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !impo…

Web3 开发者修炼全图谱:从 Web2 走向 Web3 的实用的系统性学习指南

Web3 开发者修炼全图谱:从 Web2 走向 Web3 的实用的系统性学习指南pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: &quo…

强化学习之父 Richard Sutton: 如今AI正进入“经验时代” - 指南

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

Java 注解 - 实践

Java 注解 - 实践2025-09-20 08:52 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-fami…

安规对变压器的绝缘系统要求

安规对变压器的绝缘系统要求2025-09-20 08:54 斑鸠,一生。 阅读(0) 评论(0) 收藏 举报

嵌入式笔记系列——UART:TTL-UART、RS-232、RS-422、RS-485 - 指南

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

实用指南:医院高值耗材智能化管理路径分析(下)

实用指南:医院高值耗材智能化管理路径分析(下)pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas",…

Flutter应用自动更新系统:生产环境的挑战与解决方案

Flutter应用自动更新系统:生产环境的挑战与解决方案本文基于BeeCount(蜜蜂记账)项目的实际开发经验,深入探讨Android应用自动更新的完整实现,包括GitHub Releases集成、APK安装、R8混淆问题处理等核心技术难点。项目…