【Go语言】gin + go:embed 打包静态资源文件

package main

import (
	"embed"
	"github.com/gin-gonic/gin"
	"html/template"
	"io/fs"
	"net/http"
)

var (
	//go:embed static/assets/* templates/*
	f embed.FS
)

func main() 

	app := gin.Default()

	fp, _ := fs.Sub(f, "static/assets")
	app.StaticFS("/static/assets/", http.FS(fp))

	/*
	app.GET("/static/uploads/:filename", func(ctx *gin.Context) {
		ctx.File("./static/uploads/" + ctx.Param("filename"))
	})
	*/

	tmpl := template.Must(template.New("").ParseFS(f, "templates/**/*"))
	app.SetHTMLTemplate(tmpl)

	app.GET("/", func(ctx *gin.Context) {
		ctx.HTML(http.StatusOK, "index/index.html", nil)
	})

	_ = app.Run()

}

版权声明:本文为Regulations原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。