gin
Posted chenguifeng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了gin相关的知识,希望对你有一定的参考价值。
1 package main 2 3 import ( 4 "net/http" 5 6 "github.com/gin-gonic/gin" 7 ) 8 9 func main() { 10 // Creates a gin router with default middleware: 11 // logger and recovery (crash-free) middleware 12 router := gin.Default() 13 14 router.GET("/user/:name/*action", getting) 15 16 router.Run(":8080") 17 18 } 19 20 func getting(c *gin.Context) { 21 name := c.Param("name") 22 action := c.Param("action") 23 c.String(http.StatusOK, name+" is "+action) 24 }
1 package main 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "github.com/gin-gonic/gin" 8 ) 9 10 func main() { 11 // Creates a gin router with default middleware: 12 // logger and recovery (crash-free) middleware 13 router := gin.Default() 14 15 router.GET("/welcome", getting) 16 17 router.Run(":8080") 18 19 } 20 21 func getting(c *gin.Context) { 22 name := c.DefaultQuery("name", "jack") 23 c.String(http.StatusOK, fmt.Sprintf("hello %s", name)) 24 }
登陆
1 package main 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "github.com/gin-gonic/gin" 8 ) 9 10 func main() { 11 // Creates a gin router with default middleware: 12 // logger and recovery (crash-free) middleware 13 router := gin.Default() 14 15 router.POST("/form", posting) 16 17 router.Run(":8000") 18 19 } 20 21 func posting(c *gin.Context) { 22 type1 := c.DefaultPostForm("type", "alert") 23 username := c.PostForm("username") 24 password := c.PostForm("password") 25 hobbys := c.PostFormArray("hobby") 26 c.String(http.StatusOK, 27 fmt.Sprintf("type is %s, username is %s, password is %s, hobby is %v", 28 type1, username, password, hobbys)) 29 }
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>登陆</title> 6 </head> 7 <body> 8 9 <form action="http://127.0.0.1:8000/form" method="post" enctype="application/x-wwww-form-urlencoded"> 10 用户名:<input type="text" name="username"> 11 <br> 12 密  码: <input type="password" name="password"> 13 兴趣  : 14 <input type="checkbox" value="singing" name="hobby">唱 15 <input type="checkbox" value="dancing" name="hoby">跳 16 <input type="checkbox" value="rapping" name="hoby">rap 17 <br> 18 <input type="submit" value="登陆"> 19 </form> 20 </body> 21 </html>
上传文件
1 package main 2 3 import ( 4 "fmt" 5 "log" 6 "net/http" 7 8 "github.com/gin-gonic/gin" 9 ) 10 11 func main() { 12 // Creates a gin router with default middleware: 13 // logger and recovery (crash-free) middleware 14 router := gin.Default() 15 16 router.POST("/upload", posting) 17 18 router.Run(":8000") 19 20 } 21 22 func posting(c *gin.Context) { 23 file, _ := c.FormFile("file") 24 log.Println(file.Filename) 25 26 c.SaveUploadedFile(file, file.Filename) 27 28 c.String(200, 29 fmt.Sprintf("%s upload!", file.Filename)) 30 }
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>登陆</title> 6 </head> 7 <body> 8 9 <form action="http://127.0.0.1:8000/form" method="post" enctype="application/x-wwww-form-data"> 10 头像: 11 <input type="file" name="file"> 12 <br> 13 <input type="submit" value="提交"> 14 </form> 15 </body> 16 </html>
上传多个文件
1 package main 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "github.com/gin-gonic/gin" 8 ) 9 10 func main() { 11 // Creates a gin router with default middleware: 12 // logger and recovery (crash-free) middleware 13 router := gin.Default() 14 15 router.MaxMultipartMemory = 8 << 20 16 router.POST("/upload", func(c *gin.Context) { 17 form, err := c.MultipartForm() 18 if err != nil { 19 c.String(http.StatusBadRequest, fmt.Sprintf("get err %s", err.Error())) 20 } 21 22 files := form.File["files"] 23 24 for _, file := range files { 25 26 if err := c.SaveUploadedFile(file, file.Filename); err != nil { 27 c.String(http.StatusBadRequest, fmt.Sprintf("upload err %s", err.Error())) 28 return 29 } 30 } 31 c.String(200, fmt.Sprintf("upload %d files!", len(files))) 32 }) 33 34 router.Run(":8000") 35 36 }
路由请求
1 package main 2 3 import ( 4 "fmt" 5 6 "github.com/gin-gonic/gin" 7 ) 8 9 func main() { 10 r := gin.Default() 11 //路由组1 12 v1 := r.Group("/v1") 13 { 14 v1.GET("/login", login) 15 v1.GET("submit", submit) 16 } 17 //路由组2 18 v2 := r.Group("/v2") 19 { 20 v2.POST("/login", login) 21 v2.POST("/submit", submit) 22 } 23 r.Run(":8080") 24 } 25 26 func login(c *gin.Context) { 27 name := c.DefaultQuery("name", "jack") 28 c.String(200, fmt.Sprintf("hello %s ", name)) 29 } 30 31 func submit(c *gin.Context) { 32 name := c.DefaultQuery("name", "lily") 33 c.String(200, fmt.Sprintf("hello %s ", name)) 34 }
以上是关于gin的主要内容,如果未能解决你的问题,请参考以下文章