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

标签 html go go-templates go-gin

基本信息

  • 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 有两个额外的双引号。

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

最佳答案

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

关于html - 使用 gin + go-template 的代码相同但结果不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32005459/

相关文章:

html - 无法从 html/template Golang 访问数据

javascript - Highchart 条形图中的渐变背景条

用于匹配标签最后一次出现的正则表达式

go - fmt.Println 打印出像 %s 这样的格式动词

go - 尝试使用指针字段初始化结构时出现nil指针取消引用错误

Docker swarm 尝试解析我的 compose 文件中 ENV 变量的值(因为它有一个 go 模板)并给我一个错误

go - 在Helm模板中解压Go map 值

jquery数据表没有完全占据宽度

javascript - 在特定背景颜色上创建菜单标题导致对齐出错

javascript - 仅在 Ctrl + 滚动时缩放