【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()

}

你可能感兴趣的:(gin,golang,gin,开发语言)