【问题标题】:same code but different results using gin + go-template使用 gin + go-template 的代码相同但结果不同
【发布时间】:2015-11-07 10:30:49
【问题描述】:

基本信息

  • Go 版本:go1.4.2 darwin/amd64
  • 操作系统:Mac OS X 10.10.5

我正在开发一个基于 gogin 编写的小型 Web 项目。这是我的golang代码。运行 go run test.go 后,我们有一个 Web 服务器,它正在监听 8089。

Golang test.go

package main

import "github.com/gin-gonic/gin"
import "net/http"

func main() {
    router := gin.Default()
    router.LoadHTMLGlob("templates/*")
    router.GET("/index", func(c *gin.Context) {
        c.HTML(http.StatusOK, "index.html", gin.H{
            "scheme": "http",
            "domain": "meican.loc",
        })
    })
    router.Run(":8089") // listen and serve on 0.0.0.0:8089
}

后端生成的 html 代码应包含前端 javascript 引擎(假设为 Angular.js)使用的模板。

所以模板代码在script标签中,就像这样:

部分模板/index.html

<script type="text/template" charset="utf-8">
  <div data="{{.scheme}}://{{.domain}}/qr"></div>
  <div data="{{.scheme}}://{{.domain}}/qr"></div> <!-- problem here -->
</script>

当第二次使用{{.domain}} 时,我得到了不同的结果。我刷新了浏览器并查看了源代码。然后我得到了这个:

浏览器源代码结果

<script type="text/template" charset="utf-8">
  <div data="http://meican.loc/qr"></div>
  <div data="http://"meican.loc"/qr"></div>  <!-- problems here -->
</script>

第二个div 有两个额外的双引号。

为什么会这样?以及如何解决这个问题?

【问题讨论】:

    标签: html go go-templates go-gin


    【解决方案1】:

    这是 Go 中的一个错误,截至 2016 年 3 月一直是 slated to be fixed in 1.7(在 1.6 中也是 partially addressed

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-05
      • 1970-01-01
      • 2015-10-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      • 2020-02-14
      相关资源
      最近更新 更多