Gin路由中相应数据 c.HTML()
package main
import "github.com/gin-gonic/gin"
type Students struct {
Name string `json:"name"`
Age int `json:"age"`
Sex string `json:"sex"`
}
func main() {
r := gin.Default()
r.LoadHTMLGlob("templates/*")
r.GET("/", func(c *gin.Context) {
c.String(200, "值%v", "首页")
})
r.GET("/json1", func(c *gin.Context) {
c.JSON(200, map[string]interface{}{
"success": true,
"msg": "你好Gin",
})
})
r.GET("/json2", func(c *gin.Context) {
c.JSON(200, gin.H{
"success": true,
"msg": "你好Gin H",
})
})
r.GET("/json3", func(c *gin.Context) {
stu := Students{
Name: "瓶子",
Age: 70,
Sex: "男",
}
c.JSON(200, stu)
})
r.GET("/jsonp", func(c *gin.Context) {
stu := Students{
Name: "瓶子-jsonp",
Age: 70,
Sex: "男",
}
c.JSONP(200, stu)
})
r.GET("/news", func(c *gin.Context) {
//r.LoadHTMLGlob(文件名/*)
c.HTML(200, "news.html", gin.H{
"title": "我是后台的数据",
})
})
r.GET("/goods", func(c *gin.Context) {
c.HTML(200, "goods.html", gin.H{
"title": "我是商品页面",
"price": 20,
})
})
r.Run()
}
输入h5模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h2>{{.title}}</h2>
<h2>
{{.price}}
</h2>
<br>
{{.price}}
<h5>这是一个商品页面</h5>
</body>
</html>
评论
发表评论