10
基本信息相同的代码,但不同的结果去模板
- 转到版本:go1.4.2达尔文/ AMD64
- 操作系统:Mac OS X 10.10.5
我正在致力于一个基于go和杜松子酒编写的小型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
有2个额外的双引号。
为什么会发生这种情况?以及如何解决这个问题?