默认分类

Gin embed

package main

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

// FS 下面这一行非常重要!!!!!!
//go:embed templates
var FS embed.FS

func main() {
    gin.SetMode(gin.DebugMode)

    router := gin.Default()
    tmpl := template.Must(template.New("").ParseFS(FS, "templates/*.html"))
    router.SetHTMLTemplate(tmpl)

    fe, _ := fs.Sub(FS, "static")
    router.StaticFS("/static", http.FS(fe))

    router.GET("/", func(c *gin.Context) {
        c.HTML(http.StatusOK, "index.html", gin.H{})
    })
    router.GET("/2", func(c *gin.Context) {
        c.HTML(http.StatusOK, "two.html", gin.H{})
    })
    router.Run(":8080")
}

回复

This is just a placeholder img.