最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
GolangGinembedstatic静态文件嵌入难题
时间:2026-06-24 08:35:47 编辑:袖梨 来源:一聚教程网
需求
用Gin开发Web服务时, 编译生成的应用可能如下, 需提供static目录和web-app.exe给用户
如果将static文件夹到生成的exe中,分发单个EXE文件给用户使用,更加方便
# 改进前 ├── static │ └── js/jquery.min.js │ ├── favicon.ico │ ├── index.html ├── web-app.exe # 改进后 ├── web-app.exe (static内嵌进exe里)
改进思路
a). Gin文档 静态资源嵌入 方案
参考: https://learnku.com/docs/gin-gonic/1.7/examples-bind-single-binary-with-template/11403
需要使用额外工具go-assets,操作有点复杂,因此不考虑
b). Gin 自带方法
代码:
package main
import (
"embed"
"net/http"
"github.com/gin-gonic/gin"
)
//go:embed static/*
var fs embed.FS
func main() {
r := gin.Default()
r.StaticFS("/static", http.FS(fs))
}
效果:
以favicon.ico为例, 需要访问
http://localhost/static/static/favicon.ico
中间多了两个static, 而index.html中可能资源位置是
<link rel="icon" href="/static/favicon.ico" rel="external nofollow" >

c). 改进
1. 自带http库做法
http.StripPrefix("/static", http.FileServer(http.FS(fs)))
2. 查看gin staticfs源码
看下大致调用函数名,可以看到大概是这样调用
http.FileServer(fs).ServeHTTP(c.Writer, c.Request)
func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileSystem) HandlerFunc {
absolutePath := group.calculateAbsolutePath(relativePath)
fileServer := http.StripPrefix(absolutePath, http.FileServer(fs))
return func(c *Context) {
........ 读取fs中文件,判断是否存在有权限等错误,省略
fileServer.ServeHTTP(c.Writer, c.Request)
}
}
3. 最终解决方案
package main
import (
"embed"
"net/http"
"github.com/gin-gonic/gin"
)
//go:embed static/*
var fs embed.FS
func main() {
r := gin.Default()
/*
查看staticfs方案中gin启动日志可以看到,staticfs实际注册了GET、HEAD
[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)
因此直接用r.Any
*/
r.Any("/static/*filepath", func(c *gin.Context) {
staticServer := http.FileServer(http.FS(fs))
staticServer.ServeHTTP(c.Writer, c.Request)
})
r.Run("localhost:80")
}
查看效果达到预期

总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持本站。
您可能感兴趣的文章:- Golang Gin Router冲突conflicts with existing wildcard问题
- Golang实现读取ZIP压缩包并显示Gin静态html网站
- Golang中Gin数据库表名前缀的三种方法
- golang gin ShouldBind的介绍和使用示例详解
- Golang Gin框架中间件的用法详解
- Golang中Gin框架中如何定义路由详解
- Golang 关于Gin框架请求参数的获取方法
相关文章
- 有哪些类似deepseek的软件 06-24
- 腾讯有款三国游戏叫什么 2026流行的腾讯手游排行榜 06-24
- 次元姬小说如何换绑手机号 06-24
- 《虚空之剑术士技能搭配攻略》(发挥虚空之剑的最大威力,成为无敌的剑术士!) 06-24
- centos crontab如何更改任务的执行命令 06-24
- centos crontab 怎样删除已有的任务 06-24