GO语言加载静态资源文件


GO语言加载静态资源文件

1
使用go:embed打包静态文件,防止出现文件缺失的情况,会将静态文件一起打包,导致可执行文件增大
1
2
3
4
5
6
7
8
9
10
11
//go:embed public/*
var Fs embed.FS

//静态资源加载 加载public下的所有文件 使静态资源可以通过http获取
router.Any("public/*filepath", func(context *gin.Context) {
staticServer := http.FileServer(http.FS(Fs))
staticServer.ServeHTTP(context.Writer, context.Request)
})

//加载单个文件 以下加载浏览器图标
router.StaticFileFS("/favicon.ico", "./public/static/favicon.ico", http.FS(Fs))