使用 gin + go-template 的相同代码但结果不同

same code but different results using gin + go-template

基本信息

我正在开发一个基于 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标签中,就像这样:

templates/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有2个额外的双引号。

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

这是 Go 中的一个错误,在 1.6 中已 slated to be fixed in 1.7 as of March 2016 (Also partially addressed)