Gin framework
如今多采用 GO Module 管理依赖,使用 go mod init 初始化项目, 以及 go get github.com/gin-gonic/gin 安装 Gin 框架。
Basic APIs
创建路由 + Callback 函数.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| type UserInfo struct { Name string `json:"name"` Age int `json:"age"` }
r := gin.Default()
r.LoadHTMLGlob("templates/*")
r.Static("/static", "./static")
r.Use(gin.Logger())
r.GET("/ping", func(c *gin.Context) { name := c.Query("name") userInfo := UserInfo{} c.ShouldBindJSON(&userInfo)
c.JSON(200, gin.H{ "message": "hello " + name, }) })
r.GET("/gethtml", func(c *gin.Context) { c.HTML(200, "index.html", gin.H{ "title": "Hello HTML", }) })
|