Gin学习笔记
Gin文档:https://pkg.go.dev/github.com/gin-gonic/gin
1、快速入门
1.1、安装Gin
go get -u github.com/gin-gonic/gin
1.2、main.go
package mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {// 创建路由引擎r := gin.Default()// 添加路由监听r.GET("/", func(c *gin.Context) {c.String(http.StatusOK, "Hello Gin!")})//启用 web 服务err := r.Run()if err != nil {return} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
2、配置热更新
Air文档:https://github.com/cosmtrek/air
2.1、下载
# 添加air包
go get -u github.com/cosmtrek/air
# 编译并安装air到 $GOPATH/bin 目录(重启一下Goland)
go install github.com/cosmtrek/air@latest
2.2、使用
# 直接使用air即可热加载
air
3、响应数据
3.1、String
package mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {r := gin.Default()r.LoadHTMLGlob("./template/*.html")r.GET("/", func(c *gin.Context) {c.String(http.StatusOK, "Hello Gin!")})r.GET("/hello", func(c *gin.Context) {//c.JSON(http.StatusOK, map[string]interface{}{// "name": "xumeng03",// "age": "24",//})c.JSON(http.StatusOK, gin.H{"name": "xumeng03","age": "24",})})r.GET("/gin", func(c *gin.Context) {c.HTML(http.StatusOK, "gin.html", gin.H{"path": c.FullPath(),})})err := r.Run()if err != nil {return} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
3.2、JSON
package mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {r := gin.Default()r.GET("/hello", func(c *gin.Context) {//c.JSON(http.StatusOK, map[string]interface{}{// "name": "xumeng03",// "age": "24",//})c.JSON(http.StatusOK, gin.H{"name": "xumeng03","age": "24",})})err := r.Run()if err != nil {return} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
3.3、HTML
package mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {r := gin.Default()r.LoadHTMLGlob("./template/*.html")r.GET("/gin", func(c *gin.Context) {c.HTML(http.StatusOK, "gin.html", gin.H{"path": c.FullPath(),})})err := r.Run()if err != nil {return} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Gin Study</title>
</head>
<body>
<h1>Gin Study!</h1>
<p>请求路径:{{.path}}</p>
</body>
</html>
4、请求数据
4.1、Get
1、直接查询
package mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {r := gin.Default()r.GET("/", func(c *gin.Context) {// Query 查询 request 的参数,DefaultQuery 同样查询 request 的参数但若未查询到则赋一个默认值//var name = c.Query("name")var name = c.DefaultQuery("name", "Gin")c.String(http.StatusOK, "Hello %s!", name)})err := r.Run()if err != nil {return} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
2、绑定到结构体
package mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {r := gin.Default()r.GET("/", func(c *gin.Context) {var person = Person{}err := c.ShouldBind(&person)if err != nil {c.JSON(http.StatusOK, gin.H{"status": 500,"message": "Params Error!",})return}c.JSON(http.StatusOK, gin.H{"status": 200,"data": person,})})err := r.Run()if err != nil {return} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}type Person struct {Name string `json:"name" form:"name"`Age string `json:"age" form:"age"`
}
4.2、Post
1、直接查询
package mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {r := gin.Default()r.POST("/", func(c *gin.Context) {// PostForm 查询 request 的参数,DefaultPostForm 同样查询 request 的参数但若未查询到则赋一个默认值var name = c.PostForm("name")var age = c.DefaultPostForm("age", "20")c.String(http.StatusOK, "Hello %s %s!", name, age)})err := r.Run()if err != nil {return} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
2、绑定到结构体(form-data)
package mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {r := gin.Default()r.POST("/", func(c *gin.Context) {var person = Person{}err := c.ShouldBind(&person)if err != nil {c.JSON(http.StatusOK, gin.H{"status": 500,})return}c.JSON(http.StatusOK, gin.H{"status": 500,"data": person,})})err := r.Run()if err != nil {return} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}type Person struct {Name string `json:"name" form:"name"`Age string `json:"age" form:"age"`
}
3、绑定到结构体(json)
package mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {r := gin.Default()r.POST("/json", func(c *gin.Context) {var person = Person{}err := c.ShouldBindJSON(&person)if err != nil {c.JSON(http.StatusOK, gin.H{"status": 500,})return}c.JSON(http.StatusOK, gin.H{"status": 200,"data": person,})})err := r.Run()if err != nil {return} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}type Person struct {Name string `json:"name" form:"name"`Age string `json:"age" form:"age"`
}
5、Restful
http://127.0.0.1/item/1 查询,GET
http://127.0.0.1/item 新增,POST
http://127.0.0.1/item 更新,PUT
http://127.0.0.1/item/1 删除,DELETE
package mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {r := gin.Default()r.GET("/get/:name", func(c *gin.Context) {param := c.Param("name")c.String(http.StatusOK, "Hello get %s!", param)})r.POST("/post/:name", func(c *gin.Context) {param := c.Param("name")c.String(http.StatusOK, "Hello post %s!", param)})r.PUT("/put/:name", func(c *gin.Context) {param := c.Param("name")c.String(http.StatusOK, "Hello put %s!", param)})r.DELETE("/delete/:name", func(c *gin.Context) {param := c.Param("name")c.String(http.StatusOK, "Hello delete %s!", param)})err := r.Run()if err != nil {return} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
6、路由分组
6.1、基本使用
package mainimport ("github.com/gin-gonic/gin""net/http"
)func main() {r := gin.Default()group := r.Group("/restful0"){group.GET("/get/:name", func(c *gin.Context) {param := c.Param("name")c.String(http.StatusOK, "Hello get %s!", param)})group.POST("/post/:name", func(c *gin.Context) {param := c.Param("name")c.String(http.StatusOK, "Hello post %s!", param)})}group1 := r.Group("/restful1"){group1.PUT("/put/:name", func(c *gin.Context) {param := c.Param("name")c.String(http.StatusOK, "Hello put %s!", param)})group1.DELETE("/delete/:name", func(c *gin.Context) {param := c.Param("name")c.String(http.StatusOK, "Hello delete %s!", param)})}err := r.Run()if err != nil {return} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
6.2、拆分文件
package mainimport ("ginstudy/router""github.com/gin-gonic/gin"
)func main() {r := gin.Default()router.Restful0(r)router.Restful1(r)err := r.Run()if err != nil {return} // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
package routerimport ("github.com/gin-gonic/gin""net/http"
)func Restful0(r *gin.Engine) {group := r.Group("/restful0"){group.GET("/get/:name", func(c *gin.Context) {param := c.Param("name")c.String(http.StatusOK, "Hello get %s!", param)})group.POST("/post/:name", func(c *gin.Context) {param := c.Param("name")c.String(http.StatusOK, "Hello post %s!", param)})}
}
package routerimport ("github.com/gin-gonic/gin""net/http"
)func Restful1(r *gin.Engine) {group1 := r.Group("/restful1"){group1.PUT("/put/:name", func(c *gin.Context) {param := c.Param("name")c.String(http.StatusOK, "Hello put %s!", param)})group1.DELETE("/delete/:name", func(c *gin.Context) {param := c.Param("name")c.String(http.StatusOK, "Hello delete %s!", param)})}
}