项目目录
├── asset // 静态资源文件
│ ├── bootstrap.min.css
│ ├── bootstrap.min.js
│ └── jquery.js
├── go.mod
├── go.sum
├── html // html模版文件
│ └── index.html
└── main.go
package main
import (
"embed"
"html/template"
"net/http"
"github.com/gin-gonic/gin"
)
//go:embed html/*
var htmlFS embed.FS
//go:embed asset/*
var assetFS embed.FS
func main() {
app := gin.Default()
//引入js css 例如jquery.js 访问地址为 localhost:8080/asset/jquery.js
app.Any("/asset/*filepath", func(c *gin.Context) {
staticServer := http.FileServer(http.FS(assetFS))
staticServer.ServeHTTP(c.Writer, c.Request)
})
// 引入html
app.SetHTMLTemplate(template.Must(template.New("").ParseFS(htmlFS, "html/*")))
app.Handle("GET", "/", index)
app.Run()
}
func index(c *gin.Context) {
c.HTML(http.StatusOK, "index.html")
}