douqiao7958 2015-08-14 08:21
浏览 77
已采纳

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

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 "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
}

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

<script type="text/template" charset="utf-8">
  <div data="{{.scheme}}://{{.domain}}/qr"></div>
  <div data="{{.scheme}}://{{.domain}}/qr"></div> <!-- problem here -->
</script>

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

<script type="text/template" charset="utf-8">
  <div data="http://meican.loc/qr"></div>
  <div data="http://"meican.loc"/qr"></div>  <!-- problems here -->
</script>

The second div has 2 extra double quotes.

Why this happens? And how to resolve this problem?

  • 写回答

1条回答 默认 最新

  • dongyu1979 2016-03-26 19:52
    关注

    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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型