<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                > Gin 框架默認封裝了golang內置的html/template包用于處理html模版,如果你開發的是接口服務,不提供html頁面可以跳過本章內容。 > 前置技術知識點:Go模版引擎相關教程:https://www.tizi365.com/archives/85.html [TOC] ## 返回html結果的例子 **后端代碼** ~~~ func main() { // 初始化gin對象 router := gin.Default() // 首先加載templates目錄下面的所有模版文件,模版文件擴展名隨意 router.LoadHTMLGlob("templates/*") // 綁定一個url路由 /index router.GET("/index", func(c *gin.Context) { // 通過HTML函數返回html代碼 // 第二個參數是模版文件名字 // 第三個參數是map類型,代表模版參數 // gin.H 是map[string]interface{}類型的別名 c.HTML(http.StatusOK, "index.html", gin.H{ "title": "Main website", }) }) // 啟動http服務,并且綁定在8080端口 router.Run(":8080") } ~~~ **前端代碼** > 文件名:`templates/index.html` ~~~ <html> <h1> {{ .title }} </h1> </html> ~~~ ## 處理模版子目錄的情況 > 一般在項目中,templates目錄下還會按照業務進行劃分目錄 **后端代碼** ~~~ func main() { router := gin.Default() // 加載templates目錄下面的所有模版文件,包括子目錄 // **/* 代表所有子目錄下的所有文件 router.LoadHTMLGlob("templates/**/*") router.GET("/posts/index", func(c *gin.Context) { // 子目錄的模版文件,需要加上目錄名,例如:posts/index.tmpl c.HTML(http.StatusOK, "posts/index.tmpl", gin.H{ "title": "Posts", }) }) router.GET("/users/index", func(c *gin.Context) { // 子目錄的模版文件,需要加上目錄名,例如:users/index.tmpl c.HTML(http.StatusOK, "users/index.tmpl", gin.H{ "title": "Users", }) }) router.Run(":8080") } ~~~ **前端代碼** > 文件名:`templates/posts/index.tmpl` ~~~ {{ define "posts/index.tmpl" }} <html> <h1>{{ .title }}</h1> <p>Using posts/index.tmpl</p> </html> {{ end }} ~~~ > 文件名:`templates/users/index.tmpl` ~~~ {{ define "users/index.tmpl" }} <html> <h1>{{ .title }}</h1> <p>Using users/index.tmpl</p> </html> {{ end }} ~~~
                  <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>

                              哎呀哎呀视频在线观看