官方文档

https://www.topgoer.com/gin%E6%A1%86%E6%9E%B6/%E7%AE%80%E4%BB%8B.html

安装

要安装Gin软件包,您需要安装Go并首先设置Go工作区。
1.首先需要安装Go(需要1.10+版本),然后可以使用下面的Go命令安装Gin。
go get -u github.com/gin-gonic/gin
2.将其导入您的代码中:
import “github.com/gin-gonic/gin”
3.(可选)导入net/http。例如,如果使用常量,则需要这样做http.StatusOK。
import “net/http”

入门

1.简单示例

  1. package main
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. )
  6. // apiParseReturn 解析参数,通过API解析:http://localhost:8000/hello/myName
  7. func apiParseReturn(c *gin.Context) {
  8. name := c.Param("name")
  9. c.String(http.StatusOK, "hello World:"+name)
  10. }
  11. // urlParseReturn 解析参数,通过URL解析:http://localhost:8000/hello?name=myName
  12. func urlParseReturn(c *gin.Context) {
  13. name := c.DefaultQuery("name", "陌生人")
  14. c.String(http.StatusOK, "hello World:"+name)
  15. }
  16. // GetReturnJson 返回json数据
  17. func GetReturnJson(c *gin.Context) {
  18. c.JSON(http.StatusOK, gin.H{"name": "陌生人"})
  19. }
  20. func main() {
  21. // 1.创建路由
  22. r := gin.Default()
  23. // 2.绑定路由规则,执行的函数
  24. // gin.Context,封装了request和response
  25. r.GET("/", func(c *gin.Context) {
  26. c.String(http.StatusOK, "hello World!")
  27. })
  28. //路由分组
  29. hello:=r.Group("/hello")
  30. {
  31. hello.GET("/:name", apiParseReturn)
  32. hello.GET("", urlParseReturn)
  33. hello.GET("/json", GetReturnJson)
  34. }
  35. // 3.监听端口,默认在8080
  36. // Run("里面不指定端口号默认为8080")
  37. r.Run(":8000")
  38. }

参数绑定以及响应格式(json/xml/yaml)

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "net/http"
  6. )
  7. // Login 定义接收数据的结构体
  8. type Login struct {
  9. // binding:"required"修饰的字段,若接收为空值,则报错,是必须字段
  10. User string `form:"username" json:"user" uri:"user" xml:"user" binding:"required"`
  11. Password string `form:"password" json:"password" uri:"password" xml:"password" binding:"required"`
  12. }
  13. type Response struct {
  14. Data interface{}
  15. Success bool
  16. }
  17. func main() {
  18. // 1.创建路由
  19. r := gin.Default()
  20. // 2.绑定路由规则,执行的函数
  21. // gin.Context,封装了request和response
  22. r.POST("/login", func(c *gin.Context) {
  23. var json Login
  24. if err := c.ShouldBindJSON(&json); err != nil {
  25. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  26. }
  27. //结构响应注释事项
  28. //Response{"ok", true}
  29. c.JSON(http.StatusOK, Response{"ok", true})
  30. })
  31. r.GET("/:user/:password", func(c *gin.Context) {
  32. var json Login
  33. if err := c.ShouldBindUri(&json); err != nil {
  34. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  35. }
  36. fmt.Println(json.User,json.Password)
  37. //结构响应注释事项
  38. //Response{"ok", true}
  39. c.JSON(http.StatusOK, Response{"ok", true})
  40. })
  41. //xml格式
  42. r.GET("/get/xml", func(c *gin.Context) {
  43. c.XML(http.StatusOK, Response{"ok", true})
  44. })
  45. //yaml格式
  46. r.GET("/get/yaml", func(c *gin.Context) {
  47. c.YAML(http.StatusOK, Response{"ok", true})
  48. })
  49. r.Run(":8000")
  50. }

重定向、同步、异步

  1. package main
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "log"
  5. "net/http"
  6. "time"
  7. )
  8. func main() {
  9. // 1.创建路由
  10. r := gin.Default()
  11. // 2.绑定路由规则,执行的函数
  12. // gin.Context,封装了request和response
  13. r.GET("/redirect/baidu", func(c *gin.Context) {
  14. //重定向
  15. c.Redirect(http.StatusMovedPermanently, "https://www.baidu.com")
  16. })
  17. //同步
  18. r.GET("/sync", func(c *gin.Context) {
  19. time.Sleep(3 * time.Second)
  20. log.Println("同步执行:" + c.Request.URL.Path)
  21. })
  22. //异步
  23. r.GET("/async", func(c *gin.Context) {
  24. //拷贝c
  25. copyContext := c.Copy()
  26. // 异步处理
  27. go func() {
  28. time.Sleep(3 * time.Second)
  29. log.Println("异步执行:" + copyContext.Request.URL.Path)
  30. }()
  31. })
  32. r.Run(":8080")
  33. }

同步异步区别:
image.png