Gin渲染html代码,
参考文档:https://www.kancloud.cn/shuangdeyu/gin_book/949436
后端代码:
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
//文档: https://www.kancloud.cn/shuangdeyu/gin_book/949436
func main() {
router := gin.Default()
router.LoadHTMLGlob("templates/*")
//router.LoadHTMLFiles("templates/template1.html", "templates/template2.html")
router.GET("/index", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"title": "Main website",
})
return
})
type requestDataModel struct {
Name string `json:"name"`
Pass int `json:"pass"`
Age int `json:"age"`
}
router.POST("/getData", func(c *gin.Context) {
fmt.Print(c.PostForm("name"))
var requestData requestDataModel
if err := c.BindJSON(&requestData); err == nil {
}
c.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": "success",
"data": requestData,
})
return
})
// http://127.0.0.1:8080/index
router.Run()
}
templates/index.html 代码
Title
测试渲染效果
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net