多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
**加载模板templates** gin支持加载HTML模板, 然后根据模板参数进行配置并返回相应的数据。 目录结构: ~~~ object | —— main.go | —— templates | —— index.html ~~~ 代码示例: main.go ~~~ package main import ( "net/http" "github.com/gin-gonic/gin" ) func main() { router := gin.Default() // 下面测试加载HTML: LoadHTMLTemplates // 加载templates文件夹下所有的文件 router.LoadHTMLGlob("templates/*") // 或者使用这种方法加载也是OK的: router.LoadHTMLFiles("templates/template1.html", "templates/template2.html") router.GET("/index", func(c *gin.Context) { // 注意下面将gin.H参数传入index.html中!也就是使用的是index.html模板 c.HTML(http.StatusOK, "index.html", gin.H{ "title": "GIN: 测试加载HTML模板", }) }) router.Run(":8000") } ~~~ index.html ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <h1> {{.title}} </h1> </body> </html> ~~~ 编译运行: 在浏览器请求: 0.0.0.0:8888/index 页面显示: GIN: 测试加载HTML模板 **重定向** ~~~ package main import ( "net/http" "github.com/gin-gonic/gin" ) func main() { router := gin.Default() // 下面测试重定向 router.GET("/redirect", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "https://www.baidu.com") }) router.Run(":8000") } ~~~ 编译运行: 在浏览器请求: 0.0.0.0:8888/redirect 页面跳转到百度 **binding数据** gin内置了几种数据的绑定例如JSON、XML等。即根据Body数据类型,将数据赋值到指定的结构体变量中。(类似于序列化和反序列化) 服务端代码: ~~~ package main import ( "net/http" "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" ) // Binding数据 // 注意:后面的form:user表示在form中这个字段是user,不是User, 同样json:user也是 // 注意:binding:"required"要求这个字段在client端发送的时候必须存在,否则报错! type Login struct { User string `form:"user" json:"user" binding:"required"` Password string `form:"password" json:"password" binding:"required"` } // bind JSON数据 func funcBindJSON(c *gin.Context) { var json Login // binding JSON,本质是将request中的Body中的数据按照JSON格式解析到json变量中 if c.BindJSON(&json) == nil { if json.User == "TAO" && json.Password == "123" { c.JSON(http.StatusOK, gin.H{"JSON=== status": "you are logged in"}) } else { c.JSON(http.StatusUnauthorized, gin.H{"JSON=== status": "unauthorized"}) } } else { c.JSON(404, gin.H{"JSON=== status": "binding JSON error!"}) } } // 下面测试bind FORM数据 func funcBindForm(c *gin.Context) { var form Login // 本质是将c中的request中的BODY数据解析到form中 // 方法一: 对于FORM数据直接使用Bind函数, 默认使用使用form格式解析,if c.Bind(&form) == nil // 方法二: 使用BindWith函数,如果你明确知道数据的类型 if c.BindWith(&form, binding.Form) == nil { if form.User == "TAO" && form.Password == "123" { c.JSON(http.StatusOK, gin.H{"FORM=== status": "you are logged in"}) } else { c.JSON(http.StatusUnauthorized, gin.H{"FORM=== status": "unauthorized"}) } } else { c.JSON(404, gin.H{"FORM=== status": "binding FORM error!"}) } } func main() { router := gin.Default() // 下面测试bind JSON数据 router.POST("/bindJSON", funcBindJSON) // 下面测试bind FORM数据 router.POST("/bindForm", funcBindForm) // 下面测试JSON,XML等格式的rendering router.GET("/someJSON", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "hey, budy", "status": http.StatusOK}) }) router.GET("/moreJSON", func(c *gin.Context) { // 注意:这里定义了tag指示在json中显示的是user不是User var msg struct { Name string `json:"user"` Message string Number int } msg.Name = "TAO" msg.Message = "hey, budy" msg.Number = 123 // 下面的在client的显示是"user": "TAO",不是"User": "TAO" // 所以总体的显示是:{"user": "TAO", "Message": "hey, budy", "Number": 123 c.JSON(http.StatusOK, msg) }) // 测试发送XML数据 router.GET("/someXML", func(c *gin.Context) { c.XML(http.StatusOK, gin.H{"name": "TAO", "message": "hey, budy", "status": http.StatusOK}) }) router.Run(":8000") } ~~~ 客户端代码: ~~~ package main import ( "fmt" "io/ioutil" "net/http" "strings" ) func main() { // 下面测试binding数据 // 首先测试binding-JSON, // 注意Body中的数据必须是JSON格式 resp, _ := http.Post("http://0.0.0.0:8000/bindJSON", "application/json", strings.NewReader("{\"user\":\"TAO\", \"password\": \"123\"}")) helpRead(resp) // 下面测试bind FORM数据 resp, _ = http.Post("http://0.0.0.0:8000/bindForm", "application/x-www-form-urlencoded", strings.NewReader("user=TAO&password=123")) helpRead(resp) // 下面测试接收JSON和XML数据 resp, _ = http.Get("http://0.0.0.0:8000/someJSON") helpRead(resp) resp, _ = http.Get("http://0.0.0.0:8000/moreJSON") helpRead(resp) resp, _ = http.Get("http://0.0.0.0:8000/someXML") helpRead(resp) } // 用于读取resp的body func helpRead(resp *http.Response) { defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("ERROR2!: ", err) } fmt.Println(string(body)) } ~~~