标签:pac ima use return ror alt ipa 用户名 roo
数据绑定和解析
1 package main 2 3 import ( 4 "net/http" 5 6 "github.com/gin-gonic/gin" 7 ) 8 9 type Login struct { 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: "password"` 12 } 13 14 func main() { 15 r := gin.Default() 16 //JSON绑定 17 r.POST("/loginForm", func(c *gin.Context) { 18 var form Login 19 if err := c.Bind(&form); err != nil { 20 //gin.H封装了生成JSON数据的工具 21 c.JSON(http.StatusBadRequest, gin.H{ 22 "error": err.Error(), 23 }) 24 return 25 } 26 27 if form.User != "root" || form.Password != "admin" { 28 c.JSON(http.StatusBadRequest, gin.H{ 29 "status": "304", 30 }) 31 return 32 } 33 c.JSON(http.StatusOK, gin.H{ 34 "status": "200", 35 }) 36 }) 37 r.Run(":8080") 38 }
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:8080/loginForm" method="post" enctype="multipart/form-data"> 10 用户名:<input type="text" name="username"> 11 <br> 12 密  码: <input type="password" name="password"> 13 <input type="submit" value="登陆"> 14 </form> 15 </body> 16 </html>
1 package main 2 3 import ( 4 "github.com/gin-gonic/gin" 5 "github.com/gin-gonic/gin/testdata/protoexample" 6 ) 7 8 func main() { 9 r := gin.Default() 10 //JSON 11 r.GET("/someJSON", func(c *gin.Context) { 12 c.JSON(200, gin.H{ 13 "message": "someJSON", 14 "status": 200, 15 }) 16 }) 17 18 r.GET("/someStruct", func(c *gin.Context) { 19 var msg struct { 20 Name string 21 Message string 22 Number int 23 } 24 msg.Name = "root" 25 msg.Message = "message" 26 msg.Number = 123 27 c.JSON(200, msg) 28 }) 29 30 r.GET("/someXML", func(c *gin.Context) { 31 c.XML(200, gin.H{ 32 "message": "abc", 33 }) 34 }) 35 36 r.GET("/someYAML", func(c *gin.Context) { 37 c.YAML(200, gin.H{ 38 "name": "cxk", 39 }) 40 }) 41 42 r.GET("/someProtoBuf", func(c *gin.Context) { 43 reps := []int64{int64(1), int64(2)} 44 45 label := "label" 46 47 data := &protoexample.Test{ 48 Label: &label, 49 Reps: reps, 50 } 51 c.ProtoBuf(200, data) 52 }) 53 54 r.Run(":8080") 55 }
标签:pac ima use return ror alt ipa 用户名 roo
原文地址:https://www.cnblogs.com/chenguifeng/p/11949172.html