暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

gin简单路由

go技术沙龙 2020-05-09
215

简单路由

  1. func main() {

  2. // Creates a gin router with default middleware:

  3. // logger and recovery (crash-free) middleware

  4. router := gin.Default()


  5. router.GET("/someGet", getting)

  6. router.POST("/somePost", posting)

  7. router.PUT("/somePut", putting)

  8. router.DELETE("/someDelete", deleting)

  9. router.PATCH("/somePatch", patching)

  10. router.HEAD("/someHead", head)

  11. router.OPTIONS("/someOptions", options)


  12. // By default it serves on :8080 unless a

  13. // PORT environment variable was defined.

  14. router.Run()

  15. // router.Run(":3000") for a hard coded port

  16. }

复制

路径中的参数

  1. func main() {

  2. router := gin.Default()


  3. // This handler will match user/john but will not match user/ or user

  4. router.GET("/user/:name", func(c *gin.Context) {

  5. name := c.Param("name")

  6. c.String(http.StatusOK, "Hello %s", name)

  7. })


  8. // However, this one will match user/john/ and also user/john/send

  9. // If no other routers match user/john, it will redirect to user/john/

  10. router.GET("/user/:name/*action", func(c *gin.Context) {

  11. name := c.Param("name")

  12. action := c.Param("action")

  13. message := name + " is " + action

  14. c.String(http.StatusOK, message)

  15. })


  16. // For each matched request Context will hold the route definition

  17. router.POST("/user/:name/*action", func(c *gin.Context) {

  18. c.FullPath() == "/user/:name/*action" // true

  19. })


  20. router.Run(":8080")

  21. }

复制

获取Get请求的参数

  1. func main() {

  2. router := gin.Default()


  3. // Query string parameters are parsed using the existing underlying request object.

  4. // The request responds to a url matching: welcome?firstname=Jane&lastname=Doe

  5. router.GET("/welcome", func(c *gin.Context) {

  6. firstname := c.DefaultQuery("firstname", "Guest")

  7. lastname := c.Query("lastname") // shortcut for c.Request.URL.Query().Get("lastname")


  8. c.String(http.StatusOK, "Hello %s %s", firstname, lastname)

  9. })

  10. router.Run(":8080")

  11. }

复制

获取Post请求参数

  1. func main() {

  2. router := gin.Default()


  3. router.POST("/form_post", func(c *gin.Context) {

  4. message := c.PostForm("message")

  5. nick := c.DefaultPostForm("nick", "anonymous")


  6. c.JSON(200, gin.H{

  7. "status": "posted",

  8. "message": message,

  9. "nick": nick,

  10. })

  11. })

  12. router.Run(":8080")

  13. }

复制

获取Get+Post混合参数

  1. 请求方式:

  2. POST /post?id=1234&page=1 HTTP/1.1

  3. Content-Type: application/x-www-form-urlencoded


  4. name=manu&message=this_is_great


  5. 代码:

  6. func main() {

  7. router := gin.Default()


  8. router.POST("/post", func(c *gin.Context) {


  9. id := c.Query("id")

  10. page := c.DefaultQuery("page", "0")

  11. name := c.PostForm("name")

  12. message := c.PostForm("message")


  13. fmt.Printf("id: %s; page: %s; name: %s; message: %s", id, page, name, message)

  14. })

  15. router.Run(":8080")

  16. }

复制

字典作为Get和Post请求参数

  1. 请求方式:

  2. POST /post?ids[a]=1234&ids[b]=hello HTTP/1.1

  3. Content-Type: application/x-www-form-urlencoded


  4. names[first]=thinkerou&names[second]=tianou

  5. 代码:

  6. func main() {

  7. router := gin.Default()


  8. router.POST("/post", func(c *gin.Context) {


  9. ids := c.QueryMap("ids")

  10. names := c.PostFormMap("names")


  11. fmt.Printf("ids: %v; names: %v", ids, names)

  12. })

  13. router.Run(":8080")

  14. }

复制

上传文件

单个文件
  1. 请求方式:

  2. curl -X POST http://localhost:8080/upload \

  3. -F "file=@/Users/appleboy/test.zip" \

  4. -H "Content-Type: multipart/form-data"


  5. func main() {

  6. router := gin.Default()

  7. // Set a lower memory limit for multipart forms (default is 32 MiB)

  8. // router.MaxMultipartMemory = 8 << 20 // 8 MiB

  9. router.POST("/upload", func(c *gin.Context) {

  10. // single file

  11. file, _ := c.FormFile("file")

  12. log.Println(file.Filename)


  13. // Upload the file to specific dst.

  14. // c.SaveUploadedFile(file, dst)


  15. c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))

  16. })

  17. router.Run(":8080")

  18. }

复制
多文件
  1. 请求方式:

  2. curl -X POST http://localhost:8080/upload \

  3. -F "upload[]=@/Users/appleboy/test1.zip" \

  4. -F "upload[]=@/Users/appleboy/test2.zip" \

  5. -H "Content-Type: multipart/form-data"


  6. func main() {

  7. router := gin.Default()

  8. // Set a lower memory limit for multipart forms (default is 32 MiB)

  9. // router.MaxMultipartMemory = 8 << 20 // 8 MiB

  10. router.POST("/upload", func(c *gin.Context) {

  11. // Multipart form

  12. form, _ := c.MultipartForm()

  13. files := form.File["upload[]"]


  14. for _, file := range files {

  15. log.Println(file.Filename)


  16. // Upload the file to specific dst.

  17. // c.SaveUploadedFile(file, dst)

  18. }

  19. c.String(http.StatusOK, fmt.Sprintf("%d files uploaded!", len(files)))

  20. })

  21. router.Run(":8080")

  22. }

复制


文章转载自go技术沙龙,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论