<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                **加載模板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)) } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看