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

huangapple go评论75阅读模式
英文:

same code but different results using gin + go-template

问题

基本信息

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

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

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") // 在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> <!-- 这里有问题 -->
</script>

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

浏览器源代码结果

<script type="text/template" charset="utf-8">
  <div data="http://meican.loc/qr"></div>
  <div data="http://"meican.loc"/qr"></div>  <!-- 这里有问题 -->
</script>

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

为什么会出现这种情况?如何解决这个问题?

英文:

Basic information

  • Go version: go1.4.2 darwin/amd64
  • Operating System: Mac OS X 10.10.5

I'm working on a small Web project written based on go and gin. Here is my golang code. After running go run test.go we have a web server, which is listening on 8089.

Golang test.go

package main

import &quot;github.com/gin-gonic/gin&quot;
import &quot;net/http&quot;

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

The html code generated in back-end should contain a template used by front-end javascript engine (Let's say Angular.js).

So the template code is in script tag, just like this:

Part of templates/index.html

&lt;script type=&quot;text/template&quot; charset=&quot;utf-8&quot;&gt;
  &lt;div data=&quot;{{.scheme}}://{{.domain}}/qr&quot;&gt;&lt;/div&gt;
  &lt;div data=&quot;{{.scheme}}://{{.domain}}/qr&quot;&gt;&lt;/div&gt; &lt;!-- problem here --&gt;
&lt;/script&gt;

When {{.domain}} is used at the second time, I got different result. I refreshed the browser and checked out the source code. Then I got this:

Browser source code result

&lt;script type=&quot;text/template&quot; charset=&quot;utf-8&quot;&gt;
  &lt;div data=&quot;http://meican.loc/qr&quot;&gt;&lt;/div&gt;
  &lt;div data=&quot;http://&quot;meican.loc&quot;/qr&quot;&gt;&lt;/div&gt;  &lt;!-- problems here --&gt;
&lt;/script&gt;

The second div has 2 extra double quotes.

Why this happens? And how to resolve this problem?

答案1

得分: 1

这是Go语言中的一个错误,并且已经在2016年3月被计划在1.7版本中修复(也在1.6版本中部分修复)。

英文:

This is a bug in Go, and has been slated to be fixed in 1.7 as of March 2016 (Also partially addressed in 1.6)

huangapple
  • 本文由 发表于 2015年8月14日 16:21:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/32005459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定