基本信息
- Go 版本:go1.4.2 darwin/amd64
- 操作系统:Mac OS X 10.10.5
我正在开发一个基于 go 和 gin 编写的小型 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/