代码
package main
import (
"github.com/gin-gonic/gin"
)
func _json(c *gin.Context) {
type person struct {
Name string `json:"name"`
Age int `json:"age"`
Passwd string `json:"-"`
}
p := person{"Tom", 20, "123456"}
c.JSON(200, p)
}
func _postsHtml(c *gin.Context) {
c.HTML(200, "posts/index.html", gin.H{
"say": "hello",
})
}
func _usersHtml(c *gin.Context) {
c.HTML(200, "users/index.html", gin.H{
"say": "world",
})
}
func main() {
router := gin.Default()
router.LoadHTMLGlob("templates/*/*")
router.GET("json", _json)
router.GET("posts/index", _postsHtml)
router.GET("users/index", _usersHtml)
router.StaticFile("static/picture", "static/favorite.png")
err := router.Run(":80")
if err != nil {
panic(err)
}
}
目录结构